John PHP John PHP - 3 months ago 14
Javascript Question

making cross domain request using ajax

This is probably a famous question, I have seen lot of questions similar to this on Google and so as well. There is a web server on my LAN at

10.xxx
on machine
A
.

I have a
HTML
page on machine
B
, this HTML page wants to make
ajax request
to web server at
10.xxx
, I made the page and tried this 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)


Question :


  1. Is there by any chance I can enable this in mozilla ?

  2. How can we make request to
    facebook, twitter, jquery
    lib servers via ajax if cross domain is disabled ?

  3. Can I add
    'Access-Control-Allow-Origin'
    to my vanilla ajax request ?



Thanks

Answer

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