Access-Control-Allow-Origin 浏览器js跨域问题

XMLHttpRequest cannot load http://xxxx. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://yyyy’ is therefore not allowed access.

PHP:

 

<?php
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept');
?>

 

Nginx:

在nginx.conf中配置

nginx.conf位置

$ nginx -t
http {
  ......
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers X-Requested-With;
  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}

这样就可以实现GET,POST,OPTIONS的跨域请求的支持
也可以 add_header Access-Control-Allow-Origin http://huangshiyang.com; –指定允许的url;

Node.js

app.all('*',function(req,res,next)
{
    if (!req.get('Origin')) return next();

    res.set('Access-Control-Allow-Origin','http://myapp.com');
    res.set('Access-Control-Allow-Methods','GET,POST');
    res.set('Access-Control-Allow-Headers','X-Requested-With,Content-Type');

    if ('OPTIONS' == req.method) return res.send(200);

    next();
});

I want to add CORS support to my server

For simple CORS requests, the server only needs to add the following header to its response:

Access-Control-Allow-Origin: *

There are some more headers and settings involved if you want to support verbs other than GET/POST, custom headers, or authentication. You can learn more about these options in theUsing CORS tutorial on HTML5 Rocks.

If you want the TL;DR version, take a look at the flowchart for implementing CORS support.

If you’d like to learn more about implementing CORS for a specific platform, follow one of the links below:

12 Responses so far.

  1. quest bars says:
    Excellent post. I was checking constantly this blog and I’m impressed!
    Extremely helpful info specifically the last part 🙂 I care for such information a
    lot. I was seeking this certain information for a very long time.
    Thank you and good luck.
  2. quest bars says:
    Hmm is anyone else encountering problems with the images on this
    blog loading? I’m trying to determine if its a problem
    on my end or if it’s the blog. Any responses would be greatly appreciated.
  3. quest bars says:
    Great web site you have got here.. It’s hard to find high-quality writing like yours these days.

    I truly appreciate individuals like you! Take care!!

  4. I know this if off topic but I’m looking into
    starting my own weblog and was wondering what all is required to get set up?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web smart so I’m not 100% sure. Any recommendations or advice would be greatly
    appreciated. Thanks
  5. hi!,I really like your writing very a lot! proportion we keep up a correspondence
    extra about your article on AOL? I need an expert in this house to
    unravel my problem. May be that’s you! Having a look forward to
    peer you.
  6. An outstanding share! I’ve just forwarded this onto a friend who had been conducting a little research
    on this. And he in fact bought me dinner simply because I discovered it for him…
    lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanx for spending time to discuss this topic here on your internet site.
  7. You ought to be a part of a contest for one of the greatest sites online.
    I will recommend this blog!
  8. Hi! I’ve been following your site for some time now and finally got the bravery to go ahead and give you a shout out
    from Atascocita Texas! Just wanted to tell you keep up the great job!
  9. Hello! This is my first visit to your blog! We are a
    team of volunteers and starting a new project in a community in the same niche.
    Your blog provided us useful information to work on. You have done a extraordinary job!
  10. Greetings! Very helpful advice within this article!
    It’s the little changes which will make the biggest
    changes. Many thanks for sharing!
  11. Hi! I’m at work browsing your blog from my new iphone 4! Just wanted to say I love reading through your blog and look forward to all
    your posts! Carry on the great work!
  12. Hi there, I want to subscribe for this blog to take latest updates, so where can i
    do it please help out.

LEAVE A COMMENT