Search code examples
javascripthtmlxmlhttprequestcgi-bin

xmlhttpRequest to pass URL in xmlhttp.open()


I am in a scenario where I have to pass a URL in xmlhttp.open to another server.I know that xmlhttp.open can be used to pass a URL on the same server. I want this URL to be passed on to another server and get the response back to the calling server.I had tried the following way(Scenario2):

Scenario1: Conventional way that works fine for same server:

xmlhttp.open("GET","../cgi-bin/perlcode.pl?root="+str+"&lang="+lang+"&out_notation="+out_notation,true);

Scenario2: Request to be sent to another server:

xmlhttp.open("GET","http://abcxyz.com/cgi-bin/perlcode.pl?root="+str+"&lang="+lang+"&out_notation="+out_notation,true);

In the first case I can get the response back on to the same server.In the second case I can't get the response back, but I can see that the request is passed onto the server abcxyz.con and it is processing the required result.

Question: Had I chosen a correct way.If so how to get back the response or else what are the other alternatives that I can choose with.

Note: I had chosen xmlhttp.open method so that I can get the result on the same web page without clicking submit button.


Solution

  • You can not use XMLHttpRequest to request to another server (origin), according to the Same origin policy.

    However, you can use some workarounds like:

    https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS

    http://en.wikipedia.org/wiki/JSONP