如何解决mern 应用程序:与我的 testfile.js 相比,node-fetch 在应用程序中的行为不同
当我在我的 dockerized mern 应用程序中运行此代码时,它返回
但是如果我在 javascript 文件中运行它,它会返回一个 IP 地址。
是否有任何可能的结构可以阻止来自应用程序内的 fetch 调用?或以某种方式限制它?
class PeerViewStage1 extends React.Component {
constructor() {
super();
this.state = {
data: []
}
componentDidMount() {
this.getItems() //.bind(this)
this.forceUpdate(); // update based on state
};
getItems() {
var requestOptionsAWS = {
method: 'GET',mode: 'no-cors',headers: {
'Content-Type': 'application/json/plain'
}
};
// access elastic EC2 instance public IP
fetch("http://checkip.amazonaws.com/",requestOptionsAWS)
.then((response) => {
return response.text()
})
.then((IP) => {
console.log(IP)
//check dev flag
if (globals.development_mode){
IP = "localhost"
}
fetch("http://"+IP.trim()+":8080/posts/stage1raw/")
.then(res => res.json())
.then(json => {
this.setState({
data: json.data
})
this.forceUpdate(); // update based on state
});
})
.catch(function(error) {
console.log('Requestfailed',error)
});
}
输出为应用程序:
<empty string>
输出为 file.js:
my ip adress (as xx.xx.xx.xx format)
更新 1: 我使用了模式:“no-cors” - 导致从 api 返回不透明的标头。这可能是 cors 标题问题,当我找到正确的标题时,我会更新/删除帖子。
这是我的第一篇文章,非常感谢任何反馈:D
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。