doushuangdui5419 2016-08-15 14:31
浏览 47
已采纳

如何使用AJAX创建跨域请求以及它们的工作原理

There is a web server on my LAN, at 10.xxx, on machine A.

I have an HTML page on machine B. This HTML page wants to make an AJAX request to the web server at 10.xxx. I made the page (with URL of file://abc.html) and tried it and got

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://XXXXXXXXXXX/ajax_info.txt. (Reason: CORS header 'Access-Control-Allow-Origin' missing)

My questions:

  1. Is there any way I can enable this in Mozilla?

  2. How can we make requests to Facebook, Twitter, and jQuery library servers via AJAX if cross-domain requests are prohibited?

  3. Can I add 'Access-Control-Allow-Origin' to vanilla AJAX requests?

This is probably a common question. I have seen lots of questions similar to this on Google, and SO as well.

  • 写回答

1条回答 默认 最新

  • dqssst0144 2016-08-15 14:33
    关注

    You put

    header('Access-Control-Allow-Origin: *');  
    

    on machine A

    If you want to be more secure you can add your domain in place of the * -

    http://www.foo.com for example

    header('Access-Control-Allow-Origin: http://www.foo.com');  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?