简体   繁体   English

允许远程服务器通过参数接受来自本地主机的请求

[英]Allow remote server to accept requests coming from localhost with params

I have my application running locally on my computer and it is trying to connect to my remote nodeJS/Express server.我的应用程序在我的计算机上本地运行,它正在尝试连接到我的远程 nodeJS/Express 服务器。 I have set the headers in my remote server.我已经在我的远程服务器中设置了标题。

Main question: How do I allow my remote server to accept requests coming from my localhost with parameters?主要问题:我如何允许我的远程服务器接受来自我的本地主机的带有参数的请求? People have told me that my request will not work because the requested URL and the server URL do not share the same domain.人们告诉我,我的请求将不起作用,因为请求的 URL 和服务器 URL 不共享同一个域。 The request is coming from my localhost and it is trying to access my remote node/express server.该请求来自我的本地主机,它试图访问我的远程节点/快速服务器。 This transaction works when I remove the params in the request but does NOT work when I have the params.当我删除请求中的参数时,此事务有效,但当我有参数时不起作用。

This is how I set headers in my remote server to accept all requests:这是我在远程服务器中设置标头以接受所有请求的方式:

app.use(function (req, res, next) {
  res.setHeader("Access-Control-Allow-Origin", "http://localhost:9000");
  res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

  if(res.headersSent) {
    console.log("Headers sent");
  }
  else {
    console.log("Headers not sent");
  }
  next();
});

Also, the res.headersSent keeps evaluating to false even though I can see the headers set in my Google network tab.此外,即使我可以看到在我的 Google 网络选项卡中设置的标头, res.headersSent不断评估为 false。 Why is this value returning false?为什么这个值返回false?

This is how I am making my GET request to my server from my localhost:这就是我从本地主机向服务器发出 GET 请求的方式:

var req = $http({
        url: 'https://20161128t135355-dot-jarvis-hd-live-2.appspot-preview.com/read-message',
        method: 'GET',
        cache: false,
        params: {last_updated: last_updated_time}
    });

This post was not helpful How to enable cross-origin resource sharing (CORS) in the express.js framework on node.js这篇文章没有帮助如何在 node.js 上的 express.js 框架中启用跨域资源共享 (CORS)

I think the params is causing the error, because it works fine when I take it out.我认为参数是导致错误的原因,因为当我取出它时它工作正常。 I get this error:我收到此错误:

XMLHttpRequest cannot load https://20161128t135355-dot-jarvis-hd-live-2.appspot-preview.com/read-message?last_updated=0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 502.

All the solutions say to add/set the header and as you can see I have done that but my application still gives me the same error when I include params in the request.所有解决方案都说要添加/设置标头,正如您所看到的,我已经这样做了,但是当我在请求中包含参数时,我的应用程序仍然给我同样的错误。 What should I do differently?我应该怎么做?

So that means your problem is that your server side code throws an error when you add params to the request.所以这意味着您的问题是当您向请求添加参数时,您的服务器端代码会引发错误。 You need to debug that.你需要调试它。 The CORS stuff is almost certainly irrelevant. CORS 的东西几乎可以肯定是无关紧要的。

This issue is not a CORS issue, it has something to do with my params request.这个问题不是 CORS 问题,它与我的 params 请求有关。 To fix this, I had to configure body-parser like so:为了解决这个问题,我必须像这样配置 body-parser:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

This helps when sending JSON data to the client from the server or vice versa.这有助于从服务器向客户端发送 JSON 数据,反之亦然。 You can find more information here: http://junerockwell.com/difference-parameters-query-strings-express-js/您可以在此处找到更多信息: http : //junerockwell.com/difference-parameters-query-strings-express-js/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM