我为学校项目和axios使用React-native来获取数据。 这是我的功能:
login(username, password){
axios({
method : 'post',
url : 'http://xxxx.xxxx.com/UserRestController.php',
data : {
accountId : 2
}
})
.then(function (response) {
console.log('data', response.data);
})
.catch(function (error) {
console.log(error);
});
}
当我尝试显示响应时,这始终为null。 喜欢这个
data Object {
"config": Object {
"adapter": [Function xhrAdapter],
"data": "{\"accountId\":2}",
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8",
},
"maxContentLength": -1,
"method": "post",
"timeout": 0,
"transformRequest": Object {
"0": [Function transformRequest],
},
"transformResponse": Object {
"0": [Function transformResponse],
},
"url": "http://ariary.vola.mg/UserRestController.php",
"validateStatus": [Function validateStatus],
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
},
"data": "",
"headers": Object {
"access-control-allow-origin": "*",
"connection": "keep-alive",
"content-type": "text/html; charset=UTF-8",
"date": "Mon, 31 Jul 2017 13:29:28 GMT",
"server": "nginx",
"transfer-encoding": "chunked",
"vary": "Accept-Encoding",
"x-powered-by": "EasyEngine 3.7.4",
},
"request": XMLHttpRequest {
"DONE": 4,
"HEADERS_RECEIVED": 2,
我在另一个函数中使用axios.GET,效果很好。有人能帮我吗? 感谢
答案 0 :(得分:0)
尝试解析对json的响应,然后再访问它的数据:
slice(1:3)
答案 1 :(得分:0)
经过一些测试,我的问题不是响应,而是我的数据。这始终为空。我的服务器中未定义accountId。但我不知道为什么?