问答 百科手机端

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

2023-04-29 07:50

错误消息显示:

没有“ 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);

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

提前致谢 :)

热门