[英]Two request being sent from browser but only localhost is called
I have an node js API. 我有一个节点js API。
app.post('/myapi', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Request-With");
res.header("Content-Type", "application/json");
res.header("Accept", "application/json");
* do something *
res.json({ api : "api called successfully" });
});
I have a html code placed in the public folder. 我在公共文件夹中放置了一个html代码。 I am using express to run both the html code and backend APIs. 我正在使用express来运行html代码和后端API。 My html code has request fetch as, 我的html代码已将请求提取为
var headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json, text/plain, */*');
var options = {
method: 'POST',
body: JSON.stringify(loginDetails),
headers: headers,
mode: 'no-cors'
};
fetch('http://localhost:3001/myapi', options)
.then(function (response) {
console.log(response);
res=response.statusCode;
// return response
if (res==0) {
window.location.assign("http://localhost:3001/home.html");
}
}); });
When I run the code in browser. 当我在浏览器中运行代码时。 When I click on a button that calls the above frontend code. 当我单击调用上述前端代码的按钮时。 It sends two request. 它发送两个请求。 Both the call fails. 两次呼叫均失败。 One is localhost request which is document call and other is the API call. 一种是本地主机请求,即文档调用,另一种是API调用。 But there is no response in the API call. 但是API调用中没有任何响应。
I was getting response in UI from the / call, 我正在通过/调用在UI中获得响应,
cannot POST/
So I tried adding following code, 所以我尝试添加以下代码,
app.post('/', function (req, res) {
res.json({ "status": "successfull" });
});
Now also there is two calls but UI returns {status : successfull} (output of /). 现在也有两个调用,但是UI返回{status:successl}(/的输出)。 But it is not returning output of /myapi. 但是它不返回/ myapi的输出。
Can someone please help? 有人可以帮忙吗?
I am getting an infinity initiator for localhost document. 我正在为本地主机文档获取一个无限启动器。 See the screenshot. 查看屏幕截图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.