我似乎不能让下面的Axios调用在Android上工作。它没有命中服务器并显示错误: Error: Network Error。同样的代码也适用于iOS。我需要做一些特殊的事情才能在React Native上启用Android的网络请求吗?
var url = 'https://***.****.com/api/list';
axios.get(url,
{ headers: {'Content-Type': 'application/json'}}
).then((response) => {
console.log("response = ",response.data)
})
.catch((error) => {
console.log("error = ", error)
});我在控制台中得到了这个错误:
error = > Error: Network Error
at createError (index.bundle?platfor…minify=false:117949)
at XMLHttpRequest.handleError (index.bundle?platfor…minify=false:117857)
at XMLHttpRequest.dispatchEvent (index.bundle?platfor…&minify=false:32349)
at XMLHttpRequest.setReadyState (index.bundle?platfor…&minify=false:31433)
at XMLHttpRequest.__didCompleteResponse (index.bundle?platfor…&minify=false:31260)
at index.bundle?platfor…&minify=false:31370
at RCTDeviceEventEmitter.emit (index.bundle?platfor…e&minify=false:5652)
at MessageQueue.__callFunction (index.bundle?platfor…e&minify=false:5080)
at index.bundle?platfor…e&minify=false:4793
at MessageQueue.__guard (index.bundle?platfor…e&minify=false:5034)发布于 2020-02-21 14:16:46
请不要使用"axios“而不是"fetch”。它适用于iOS和安卓系统,下面是获取数据的示例代码。
Test = async ()=>
{
try
{
fetch(
'your url',
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue',
})
}).then(response => {
const statusCode = response.status;
const data = response.json();
return Promise.all([statusCode, data]);
})
.then((res) => {
if (res[0] == 200 && res[2].request_results.result_header.code == 100)
{
}
}
}).catch((error) =>
{
console.error(error);
}
);
}
}
catch (e)
{
}
};您将在"res“中获取数据,并可以使用for-loop或其他您想要应用的逻辑来提取数据。
https://stackoverflow.com/questions/59847802
复制相似问题