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.



header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept');





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

也可以 add_header Access-Control-Allow-Origin; –指定允许的url;


    if (!req.get('Origin')) return next();


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


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 thoughts on “Access-Control-Allow-Origin 浏览器js跨域问题

  1. 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. 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. 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

  4. 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.

  5. 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.

  6. 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!

Leave a Reply

Your email address will not be published. Required fields are marked *