最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested res

programmeradmin4浏览0评论

I am using apache httpd server for hosting client side files

http://ipaddress:8010/

and my Nodejs server is running on http://ipaddress:8087

when i am sending post request then it shows following error

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.

my client side code is :

    $.ajax({
  type: "POST",

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

my server side is:

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

options allowed still it is not working can anybody suggest what exactly is the problem? i am receiving request on server side but not able to send any response.

thanks in advance :)

I am using apache httpd server for hosting client side files

http://ipaddress:8010/

and my Nodejs server is running on http://ipaddress:8087

when i am sending post request then it shows following error

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.

my client side code is :

    $.ajax({
  type: "POST",

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

my server side is:

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

options allowed still it is not working can anybody suggest what exactly is the problem? i am receiving request on server side but not able to send any response.

thanks in advance :)

Share Improve this question asked Aug 22, 2014 at 10:35 RahulRahul 7101 gold badge10 silver badges26 bronze badges 1
  • 1 en.wikipedia.org/wiki/Same-origin_policy – Adil Shaikh Commented Aug 22, 2014 at 10:36
Add a comment  | 

2 Answers 2

Reset to default 9

The error message says:

No 'Access-Control-Allow-Origin' header

You have set three Access-Control-Allow-SOMETHING headers, but none of them is Origin.

You will need to enable CORS.

To add CORS headers in apache add the following line inside either the <Directory>, <Location>, <Files> or <VirtualHost> section of server config file or within a .htaccess file:

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

refer to this link for apache

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

For express you can add middleware that will set required headers for response

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论