Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request content on another domain/server

I'm trying to request data which is located on another domain/server, but I'm getting an exception when I try to send the request.

var request = new XMLHttpRequest();
request.open("GET", "http://www.w3schools.com/ajax/cd_catalog.xml", false);
request.send();

The error:

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)" nsresult: "0x80004005 (NS_ERROR_FAILURE)"]

Is this the correct way to request content which isn't on the same domain/server? Or is there some other way to accomplish this?

I'm testing this in firefox 8.0, but I'd like a solution that could work for all major modern browsers.

like image 509
helloworld922 Avatar asked May 04 '26 07:05

helloworld922


1 Answers

This is called Cross-Domain Ajax most browsers consider this a security violation. One workaround is to create a server side component (the same domain as the page you are viewing) that will request the data from the other server (/www.w3schools.com in your case) and echo that back to your Ajax request.

these links will explain the problem and several solutions:

http://jimbojw.com/wiki/index.php?title=Introduction_to_Cross-Domain_Ajaxrequest

http://usejquery.com/posts/the-jquery-cross-domain-ajax-guide

like image 117
Steve Robillard Avatar answered May 06 '26 20:05

Steve Robillard