[ACCEPTED]-Can I use XMLHttpRequest on a different port from a script file loaded from that port?-cross-domain

Accepted answer
Score: 10

Nope- can't do this with XHR. Same-domain 4 policy is very restrictive there- same host, same 3 port, same protocol. Sorry! You'll have 2 to resort to other tricks (iframes, title 1 manipulation, etc) to get it to work.

Score: 10

You can do this by adding Access-Control-Allow-Origin header.

If you 2 are using PHP

header("Access-Control-Allow-Origin: http://example.com");

or in Node.js

response.writeHead(200, {'Access-Control-Allow-Origin':' http://example.com'});

This should do 1 the trick for you. It always works for me.

Score: 1

I just solved a similar issue with a PHP 7 service I'm currently playing around with 6 (not sure how relevant a PHP solution is 5 to this directly, but...) by making a single 4 line proxy PHP page, SimpleProxy.php:

<?php
echo file_get_contents('http://localhost:4567');
?>

And 3 in my XMLHttpRequest I use 'SimpleProxy.php' in 2 place of 'http://localhost:4567', which effectively puts the 1 request on the same domain as my .js code.

More Related questions