【发布时间】:2017-05-11 21:08:58
【问题描述】:
我认为这可能是一个非常简单的问题,但我是网络请求的新手,可以让它工作,也不能在网络上得到简单的响应
我有一个站点,我可以通过将这个 URL 放入浏览器来获得 JSON 响应:http://www.test.com/callservice.php?action=stop&x=1&y=2&ct=100
这反过来又给了我一些 JSON 响应。
现在,我正在尝试使用 Axios 在 Javascript 中获得相同的效果。
直接使用网址
componentWillMount() {
axios.get('http://www.test.com/callservice.php?action=stop&x=1&y=2&ct=100')
.then(response => console.log(response));
}
或者通过使用 GET 参数:
componentWillMount() {
axios.get('http://www.test.com/callservice.php', {
params: {
action: 'stop',
x: 1,
y: 2,
ct=100
}
})
.then(response => console.log(response));
}
但是两种方法都会给出相同的错误:
Possible Unhandled Promise Rejection (id: 0):
Network Error
Error: Network Error
还有一个更详细的捕获错误:
Error: Network Error
at createError (createError.js:15)
at XMLHttpRequest.handleError (xhr.js:87)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:542)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:378)
at XMLHttpRequest.js:482
at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
at MessageQueue.__callFunction (MessageQueue.js:236)
at MessageQueue.js:108
at guard (MessageQueue.js:46)
【问题讨论】:
-
尝试添加一个catch子句来查看错误:axios.get('testsite.com/pl/ite/…).then(response => console.log(response)).catch(e => console.log( e));
-
@DiogoSgrillo 我这样做了,并在问题中添加了完整的错误,但我仍然无法理解问题所在。
-
官方文档格式响应不同。你可以试试这个格式吗? .then(function (response) { console.log(response); })
-
@SergChernata 你能澄清一下我该怎么做吗?
-
另外,我已经更改为真实的 URL,以防它有助于调试。
标签: javascript json react-native httprequest axios