13

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!

Community
  • 1
  • 1
patricksweeney
  • 3,939
  • 7
  • 41
  • 54

3 Answers3

13

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
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 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 Aug 08 '10 at 18:40
  • 1
    @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 Aug 08 '10 at 18:41
  • 1
    So basically I should see if we can wrap the server response in JSONP or use a proxy, right? – patricksweeney Aug 08 '10 at 18:42
  • @patricksweeney - Correct, those are your two best solutions here :) – Nick Craver Aug 08 '10 at 18:49
  • I'm trying to get NexTrip API via json but its returning an XML file. Am I going crazy? – ericjam Sep 02 '12 at 01:43
2

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({
    ..
});

For this to work, you would need control of the server. The server has to know that XML is requested, and respond with a function call which contains the XML string as a parameter. Assuming the callback name you sent over to the remote server was foo, the server will have to respond with something like:

foo("<names><name>..</name></names>")

I think if you were using a browser that supported E4X, then there would be no need to wrap the XML inside a string. The server could simply return the XML as an argument to the callback function:

foo(
  <names>
    <name>John Doe</name>
  </names>
)

But unfortunately, E4X is not widely supported yet.

Anurag
  • 140,337
  • 36
  • 221
  • 257
  • 3
    jQuery does support xmlp if you set `dataType` to `jsonp xml`. Per the docs: "As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml." – Grae Kindel Dec 29 '11 at 21:26
  • @greg.kindel - thanks for the info. I'll **try** to update the answer, but I'm feeling kinda lazy right now. – Anurag Dec 29 '11 at 23:03
-3

You can write XML in Javascript function inside in /* comment */ and convert this function to text with method functionname.toString() and parsing text between "/*" and "*/" with JSONP's callback function, that works in all old browsers. Example xml_via_jsonp.js :

function myfunc()
{/*
<xml>
<div class="container">
        <div class="panel panel-info col-lg-10 col-lg-offset-1 added-panel">
            <div class="panel-heading">Random1 - Random2</div>
            <div class="panel-body">
                <div>Random3</div>
            </div>
        </div>
    </div>
</xml>
*/}

function callback(func)
{
var myhtml = func.toString();
var htmlstart = myhtml.indexOf('/*');
var htmlend = myhtml.lastIndexOf('*/');
return myhtml.substr(htmlstart+2, htmlend-htmlstart-2);
}
Derozer
  • 69
  • 5