Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 从react native调用Elastic Beanstalk服务_React Native_Amazon Elastic Beanstalk - Fatal编程技术网

React native 从react native调用Elastic Beanstalk服务

React native 从react native调用Elastic Beanstalk服务,react-native,amazon-elastic-beanstalk,React Native,Amazon Elastic Beanstalk,我创建了一个Elastic Beanstalk服务,可以使用ebopen在浏览器中打开它 但是,当我将URL放入我的React本机代码中以便向我的服务发出获取请求时,我得到以下错误: TypeError: Network request failed at XMLHttpRequest.xhr.onerror (fetch.js:441) at XMLHttpRequest.dispatchEvent (event-target.js:172) at XMLHttpRequest.setRead

我创建了一个Elastic Beanstalk服务,可以使用ebopen在浏览器中打开它

但是,当我将URL放入我的React本机代码中以便向我的服务发出获取请求时,我得到以下错误:

TypeError: Network request failed
at XMLHttpRequest.xhr.onerror (fetch.js:441)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:567)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:397)
at XMLHttpRequest.js:503
at RCTDeviceEventEmitter.emit (EventEmitter.js:179)
at MessageQueue.__callFunction (MessageQueue.js:351)
at MessageQueue.js:116
at MessageQueue.__guardSafe (MessageQueue.js:314)
at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:115)
我的代码如下所示:

class IntroScreen extends React.Component {
sendDataToBackend = async () => {
 fetch('https://<URL>').then(function(response) {
    if (!response.ok) {
        console.log(response.statusText);
    }
    return response;
}).then(function(response) {
    console.log("ok");
}).catch(function(error) {
    console.log(error);
});
}

render() {

return (

    <View style={tutorialStyle.buttonContainer}>
      <Button
        title=">"
        color="orange"
        height="40"
        onPress={() => this.sendDataToBackend()}
      />
    </View>
);
}
}
class IntroScreen扩展了React.Component{
sendDataToBackend=async()=>{
获取('https://')。然后(函数(响应){
如果(!response.ok){
console.log(response.statusText);
}
返回响应;
}).然后(功能(响应){
控制台日志(“ok”);
}).catch(函数(错误){
console.log(错误);
});
}
render(){
返回(
this.sendDataToBackend()}
/>
);
}
}

向客户提供的react本机代码是否与EB服务来自同一来源

如果不是,您得到的错误可能是由浏览器引起的,浏览器通常会限制这一点:(CORS)。要允许它,您必须更改Elastic Beanstalk(EB)服务的标头

根据您的EB操作系统(Linux或Windows)和web服务器,您可以相应地设置服务应用程序的标头以允许跨来源呼叫

使用Elastic Beanstalk在Apache web服务器上为容器化服务器设置它的方法。演示如何允许cors用于弹性beanstalk nginx安装

如果您认为cors是导致错误的原因,您可以尝试通过更多搜索来找到帮助或解决方案。或者你可以用你的特定操作系统和应用程序设置信息(哪个操作系统,你使用哪个应用程序框架,弹性豆茎细节(负载平衡与否,等等))创建一个新问题,我们可以尝试并指导你修复cors的最佳方法。你提供的细节越多,我们就越能帮助你