我使用原生基础
在 iOS 应用程序上工作request
.get('http://url_with_query')
.then(function(res){
console.log(res.body.data);
})
.catch(function(err){
console.log(err);
})
相同请求在 React Web应用程序中正常运行,但无法在 React-Native
中运行React-Native调试控制台 - 结果如下
Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
at Request.crossDomainError (client.js:616)
at XMLHttpRequest.xhr.onreadystatechange (client.js:724)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:534)
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)
React Console - 结果
Response {req: Request, xhr: XMLHttpRequest, text: "{"data":[{"stock":"in stock","min_sale_qty":5,"max…_description":"Consumer Products"}],"status":200}", statusText: "OK", statusCode: 200…}
目标网址标题
header("Content-Type:application/json");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');