XMLHttpRequest无法加载所请求的资源上没有“ Access-Control-Allow-Origin”标头因此,不允许访问原点

【字号: 日期:2024-04-16浏览:45作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决XMLHttpRequest无法加载所请求的资源上没有“ Access-Control-Allow-Origin”标头因此,不允许访问原点?

错误消息显示:

没有“ Access-Control-Allow-Origin”标头

您已经设置了三个Access-Control-Allow-SOMETHING标头,但都不是Origin。

解决方法

我正在使用Apache httpd服务器托管客户端文件

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);

允许的选项仍然不起作用,有人可以建议问题出在哪里吗?我在服务器端收到请求,但无法发送任何响应。

提前致谢 :)

相关文章: