如何使用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.

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