views:

44

answers:

2

I asked an earlier question which was definitely helpful and let me know about JSONP. However, I see that I have to specify JSONP as the datatype. Now, as far as I know, that is the return type of the data coming back, which would be XML. Can XML be returned using JSONP or am I limited to it coming back as JSONP format? Thanks!

+2  A: 

You're limited to JSONP (and not XML) because of how it works. JSONP turns into this:

<script src="myPage?callback=myFunction" type="text/javscript">

So when you take the content, it's effectively doing this:

<script type="text/javascript">
  myFunction({ data: value, data2: value2 });
</script>

What comes back is actual running JavaScript, so it can't be XML, you'll get all sorts of syntax errors, exactly as you would doing this:

<script type="text/javascript">
  <elem>
    <data>value</data>
    <data2>value2</data2>
  </elem>
</script>

As you can imagine, the JavaScript parser isn't going to like that very much, and doesn't know what to do with it. jQuery can parse XML in most cases without any trouble, but if you're using JSONP and it's for cross-domain requests...well JSONP is your only option there, unless you wrote a proxy page on your site that didn't violate same-origin policy rules, and used it as a proxy to fetch the XML through.

Nick Craver
Thanks for the help! If I wanted to POST the data to the API _without_ using JSONP, I could just POST the data to the API and not have to worry about the weird cross domain stuff correct? Thanks for the help, I'm a php guy and Javascript/jQuery is still pretty new to me! Basically I am trying to write a widget to put on other sites, so I am limited to Javascript, and can't use anything server side. Thanks again!
patricksweeney
@patricksweeney - You could post yes, but you won't get a response back, it's the response *from* the server, not a POST *to* the server that your browser blocks...the content of the POST response would be empty, that's how the same-origin security works in every major browser.
Nick Craver
So basically I should see if we can wrap the server response in JSONP or use a proxy, right?
patricksweeney
@patricksweeney - Correct, those are your two best solutions here :)
Nick Craver
A: 

The idea is to send back executable code from the server. Write a jQuery plugin or extend the ajax function to return the XML string as a function parameter.

myCallback("
  <root>
    <person>
      <first>John</first>
      <last>Doe</last>
    </person>
  </root>")

The plugin will parse this string to XML and return it back to your actual callback. As far as your callback is concerned, it is unaware of the string -> xml conversion process.

Here's an existing implementation.

The most ideal interface to this with jQuery would be,

$.ajax({
    url: 'http://example.com/resource?type=xml',
    dataType: 'xmlp',
    success: function(xml) { .. }
});

but since messing around and rewriting jQuery.ajax is problematic, you could write this as a separate namespaced plugin itself which will use getScript underneath.

$.myNamespace.ajax({
    ..
});
Anurag