3

in my JS project I need to load data from cross-domain. (JavaScript sits on domain A, the data comes from domain B)

I have a solution that uses JSONP but I really need to load an XML instead (ordinary XML music playlist). The main goal is to be able to load and parse the XML data without the need to modify them first to some other format (like JSONP).

Is it completely impossible? Or are there any workarounds or hacks?

I am targeting mainly the latest browsers mainly on iOS.

Thanks!

PS: Could easyXDM be of any help? Or it's not relevant to XMLs?

UPDATE: unfortunately I can not use proxy, I am really asking about a direct solution.

daniel.sedlacek
  • 8,129
  • 9
  • 46
  • 77
  • easyXDM can definitely be of help - have you seen the [XHR](http://consumer.easyxdm.net/current/example/xhr.html) demo? – Sean Kinsey Apr 08 '11 at 10:12

4 Answers4

2

You can totally do this, just have your domain B return something like

func("<myxml></myxml>");

or

var someVar = "<myxml></myxml>";

The name JSONP doesn't really have anything to do with JSON specifically since its concept is all about executing JavaScript that has your data embedded in the code.

Once your domain B returns exactly one of those 2 forms above, domain A can simply use it either by doing:

<script>
function func(xmlString) {
    alert(xmlString); // you can parse the xmlString with 
                      // jQuery or something else
}
</script>

or if you use the second example:

<script>
alert(someVar);
</script>
Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
1

The usual solution is to have a "AJAX proxy" - a simple server-side script running on your domain, that fetches the data from the other domain and returns it unchanged.

The simplist is to give the script the URL you need the data from:

http://example.com/proxy.php?url=http%3A%2F%2Fexample.org%2Fajax%3Fid%3D123 gets the data from http://example.org/ajax?id=123

This can however be misused if you let any URL be fetched like that, so you should have your script, check that it actually only gets data from a specific URL.

In order to avoid having to parse the URL to check this, you could write a proxy specificly for your app, that only accesses the specific resource you need:

http://example.com/proxy.php?id=123 to access http://example.org/ajax?id=123.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • Thanks for your suggestion, unfortunately I can not use the proxy, I was really wondering if there is any direct solution. I will update the question. – daniel.sedlacek Apr 08 '11 at 08:42
1

If you have a JSON-P solution in place, you can just pass the XML to the JSON-P callback as a string. You can then do XML parsing of a variable string in JavaScript

Community
  • 1
  • 1
monsur
  • 45,581
  • 16
  • 101
  • 95
  • what will that require on the server side? can the XML be packed as JSONP callback "on the fly"? – daniel.sedlacek Apr 08 '11 at 08:41
  • Lets say your server has the xml response in a variable called "xml", and the name of the JSON-P callback is "callback". Then, your server will have to do something like (and this is pseudocode): `print callback + "('" + escapeForJs(serializeXmlToString(xml)) + "');";` The answer from Luca Matteis also covers this. – monsur Apr 08 '11 at 19:37
1

The whole idea with JSONP is that the response must be executable as script. So sure, you can pass XML data back, as long as it's valid Javascript - for example, the server could wrap its response in a string:

myCallback('<xml><stuff/></xml>')

and you'd have to parse it with jQuery:

success: function(data) { 
    var xml = $(data); // now do stuff 
}

This assumes that you control the other server and/or someone who does is interested in formatting their data that way. Otherwise, you're out of luck, and need a proxy of some sort - you might be able to do this with YQL.

nrabinowitz
  • 55,314
  • 10
  • 149
  • 165
  • Exactly what i thought , thank you very much for clarifying. i will choose it tomorrow. since i have to wait 9 min....:) – Royi Namir Oct 04 '11 at 21:52
  • I guess the server admin prefer to send json objects instead of xml... only because of the size ?? – Royi Namir Oct 04 '11 at 21:57
  • There are lots of reasons to format a server response as JSON - size is one, but it's also easier to generate in many languages, and, as this question shows, JSON is also valid Javascript code, so it can be used easily in a JSONP request. – nrabinowitz Oct 04 '11 at 21:59
  • when you say :"so it can be used easily in a JSONP request...." you mean that after i got the response - i dont have to analyze the object as if it was XML. i can just use it as regular JS object... Is that what you meant ? – Royi Namir Oct 04 '11 at 22:04
  • I mean it doesn't require any post-processing. If you send XML in a string, as in my answer, it then needs to be parsed to be usable in Javascript. If the server responds with JSON, e.g. `callback({ "success": 1 })`, it's already valid Javascript - no parsing necessary. – nrabinowitz Oct 04 '11 at 22:22