为什么简单axios无法在React Native上运行

时间:2019-04-24 06:08:17

标签: reactjs react-native graphql axios

我试图在Expo Native android客户端React Native上使用axios来获取数据。我写道:

axios({
    url: 'http://my_local_id/graphql',
    method: 'get',
    data: {
      query: `
      query allPosts {
        posts(listType: "all") {
          text
        }
       }
      `,
    }
  }).then(result => {
    console.log(result.data);
  }).catch(function (error) {
    console.log(error);
    console.log(error.request._response);
  });

该错误给了我一个日志:“请求失败,状态码为400”和一些其他消息

堆栈跟踪:createError中的node_modules \ axios \ lib \ core \ createError.js:16:24:set节点中的node_modules \ axios \ lib \ core \ settle.js:19:6句柄中的js:77:13加载load_node_modules \ event-target-shim \ lib \ event-target.js:172:43在dispatchEvent node_modules \ react-native \ Libraries \ Network \ XMLHttpRequest.js:570:23在setReadyState node_modules \ react中__didCompleteResponse node_modules \ react-native \ Libraries \ vendor \ emitter \ EventEmitter.js:191:12中的-native \ Libraries \ Network \ XMLHttpRequest.js:392:25发出node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js __callFunction node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js中的:349:47:node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js中的106:26:__ guard __guard node_modules \ react-native \ Libraries中的\ callFunctionReturnFlushedQueue中的\ BatchedBridge \ MessageQueue.js:105:17 [本地代码]:callFunctionReturnFlushedQueue中的null

我查看了来自error.request._response的消息,它显示为“ message”:“ POST正文发送了无效的JSON。” 但是我只发送了“获取”请求。我不知道这是怎么了。下面是我的package.json文件。

"axios": "^0.18.0",
 "expo": "^32.0.0",
 "react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",

0 个答案:

没有答案