XMLHttpRequest无法加载。 请求的资源上没有“Access-Control-Allow-Origin”标题。 因此,原产地不得进入

我使用Apache HTTPD服务器托pipe客户端文件

http://ipaddress:8010/ 

和我的Nodejs服务器上运行http://ipaddress:8087

当我发送邮件请求,然后显示以下错误

 XMLHttpRequest cannot load http://ipaddress:8010/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://ipaddress:8087' is therefore not allowed access. 

我的客户端代码是:

  $.ajax({ type: "POST", url: "http://ipaddress:8010", data: {name:"xyz"}, success: function(){ alert("success"); }, dataType: "json" }); 

我的服务器端是:

 response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); response.setHeader('Access-Control-Allow-Credentials', true); 

选项允许仍然是行不通的任何人都可以build议究竟是什么问题? 我在服务器端收到请求,但无法发送任何响应。

提前致谢 :)

错误消息说:

没有“访问控制允许来源”标题

您已经设置了三个Access-Control-Allow-SOMETHING标题,但没有一个是Origin

您将需要启用CORS。

要在apache中添加CORS头文件,请在服务器configuration文件的<Directory><Location><Files><VirtualHost>部分或.htaccess文件内添加以下行:

 Header set Access-Control-Allow-Origin "*" 

参考这个链接的Apache

http://enable-cors.org/server_apache.html

为了expression,你可以添加中间件来设置响应所需的头文件

 app.use(function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); next(); }); 

http://enable-cors.org/server_expressjs.html