Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.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
Javascript 从浏览器发送两个请求,但只调用localhost_Javascript_Node.js_Rest_Api - Fatal编程技术网

Javascript 从浏览器发送两个请求,但只调用localhost

Javascript 从浏览器发送两个请求,但只调用localhost,javascript,node.js,rest,api,Javascript,Node.js,Rest,Api,我有一个节点JSAPI 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",

我有一个节点JSAPI

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" });
});
我有一个html代码放在公用文件夹中。我使用express来运行html代码和后端API。 我的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");
  }
}))

当我在浏览器中运行代码时。当我点击调用上述前端代码的按钮时。它发送两个请求。两次通话都失败了。一个是localhost请求,它是文档调用,另一个是API调用。但是API调用中没有响应

我在UI中收到来自/呼叫的响应

cannot POST/
所以我尝试添加以下代码

app.post('/', function (req, res) {
    res.json({ "status": "successfull" });
});
现在还有两个调用,但UI返回{status:successfull}(输出/)。但它不会返回/myapi的输出

有人能帮忙吗

我得到了一个本地主机文档的无限启动器。请看屏幕截图。

在请求中附加任何唯一参数,以避免发送请求的缓存版本

/api/name?id=


您可以找到更多详细信息。

您好,您有错误消息要共享吗?您好,我没有收到错误消息。但是对/myapi的调用被取消,但浏览器正在加载“localhost:port/”响应。两者都在同一来源。使用您的代码,我无法重新创建问题。我能够收到请求。您是否有其他详细信息?问题不在于缓存的请求。考虑到响应
cannot POST/