简体   繁体   English

浏览器发送了两个请求,但只有本地主机被调用

[英]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. 查看屏幕截图。 在此处输入图片说明

Append any unique parameter to request to avoid sending cached version of request. 将唯一的参数附加到请求中,以避免发送请求的缓存版本。

/api/name?id=<unique_param>

You can find more details here and here . 您可以在此处此处找到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个仅从JS访问且不会随每个请求/发送的Cookie。 - Create a cookie that is accesibe from JS only and no being sent with every request/ 没有从JQuery发送Ajax请求 - Ajax request not being sent from JQuery 如何从通过 Fetch API 发送的后端 (localhost) 获取数据? (PHP、Fetch、React Native、POST 请求) - How do I fetch data from my back-end (localhost) that's being sent through Fetch API? (PHP, Fetch, React Native, POST request) 发出PUT请求会导致在PUT之前先发送OPTIONS请求,为什么浏览器会这样操作? - Making a PUT request results in an OPTIONS request being sent first before the PUT, why does the browser behave this way? CORS 错误是否会阻止发送请求? - Do CORS errors prevent a request from being sent? Mootools:发送请求时,如何阻止浏览器冻结 - Mootools: How to stop browser from freezing when Request is sent 为什么从浏览器到本地主机的JavaScript请求没有被阻止? - Why is a request through JavaScript from a browser to localhost not blocked? 如何在没有 JQUERY 的情况下从浏览器扩展向 localhost 发出 POST 请求? - How to do a POST request from a browser extension to localhost WITHOUT JQUERY? 请求两次发送到服务器 - Request is being sent to server twice 未发送 Jquery POST 请求 - Jquery POST request not being sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM