Saturday, March 13, 2010

jQuery -- Returning XML with ajax

Over the past week I have been working on a project where I am consuming RESTful web services with jQuery's ajax feature (http://api.jquery.com/jQuery.ajax/). Because I don't have a lot of experience with jQuery and RESTful web services I decided that I would use jsMockito (http://jsmockito.org/) and Qunit (jQuery's unit testing framework) to verify that I can successfully consume the web service with jQuery. When I was running my unit tests I started seeing weird error messages in firefox. The error was something like:

XML parsing error: no element found {file name} Line Number 1, Column 1

I started up fiddler in firefox so that I could see the data being sent to and from the server and was able to identify that the XML was actually being returned and that it was valid XML. After some additional research online I found out that jQuery doesn't allow you to retrieve XML from a domain other than teh one you are running on. This means that if you are running your application on http://myCompany then you will not be able to retrieve data (using XML as the dataType) from http://anotherCompany/. After additional analysis I found that you can set the dataType to either script or json in order to be able to retrieve data from a service that is not on the same domain.

No comments:

Post a Comment