I am new to JavaScript and Ajax.
I have a form which posts to remote url and returns a XML response containing some data and a URL. I need to extract the url and redirec
Yes, due to the same origin policy restriction you cannot use javascript to perform this request. You could write a server side script that will perform the request to fetch the remote resource and then use an AJAX request to this server side script on your domain that will bring the XML and allow you to parse it.
Obviously depending on the server side language you are using there might be different ways to implement this.
But if you want to use jQuery it is pretty easy once you have setup this server side script on your domain. You simply use the $.ajax() method:
$.ajax({
url: '/url_to_your_server_side_script_that_serves_as_a_bridge',
success: function(xml) {
alert($('Result', xml).text());
}
});
And here's a live demo.
I agree that jQuery is a good way to go about this and I recently discovered this very handy plug-in which will allow you to retrieve data from other sites. Perhaps you can grab the url this way and then work with it?
http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
Yes, all ajax calls have to be to the same domain as your web page; browsers prohibit you from making ajax calls to remote addresses, with or without jQuery.
The most common way around this is to use a simple proxy on your end. You call your own proxy—which is on your domain—and the proxy creates a web request to the remote address, and when that returns, the proxy forwards that result to the callback of your ajax request. The proxy is basically just a big dumb middleman.
You can also get around with by using JSONP, but the remote end has to be set up for this.
Silverlight can also get you around this, but again, the other end has to be set up for this; they have to have a clientaccesspolicy.xml file in place.