PDA

View Full Version : How to use JsonP proxy with xml response.



tavinashb
1 Jun 2012, 12:23 AM
I am sending request to cross domain using JsonP proxy and response to this request is in xml.
But, after request completion, i am getting following error in firebug -
"unterminated regular expression literal".

Can you help me with using JsonP proxy with xml response?

scottmartin
1 Jun 2012, 2:10 PM
Can you set the content type?
response.setContentType("application/xml");

Scott.

skirtle
2 Jun 2012, 12:13 PM
I suggest you do a little more research into how JsonP works. It is not a magic solution to cross-origin problems, it uses a number of tricks to achieve the effect and that greatly limits what is possible.

JsonP overcomes origin restrictions by injecting a script tag into the page. The returned data will then be run as JavaScript. It doesn't make sense to return XML as that is not valid JavaScript.

If you wanted to handle XML it would need to be wrapped as a JavaScript string and passed to the callback function. You'd then have to write a custom reader to handle it. It'd be very complicated and not really worth the effort. It would require server changes to do the wrapping and if you have the ability to make server changes then you should just have it return valid JsonP in the first place.

Another alternative is to use CORS. That would allow you to work with straight XML but you'd have to assess whether it meets your browser requirements as it isn't supported by all browsers.