JavaScript, JSONP and reading XML from cross-domain - Stack Overflow

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

in my JS project I need to load data from cross-domain. (JavaScript sits on domain A, the data es 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 pletely 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.

in my JS project I need to load data from cross-domain. (JavaScript sits on domain A, the data es 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 pletely 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.

Share Improve this question edited Apr 8, 2011 at 8:44 daniel.sedlacek asked Apr 7, 2011 at 9:29 daniel.sedlacekdaniel.sedlacek 8,64910 gold badges48 silver badges80 bronze badges 1
  • easyXDM can definitely be of help - have you seen the XHR demo? – Sean Kinsey Commented Apr 8, 2011 at 10:12
Add a ment  | 

4 Answers 4

Reset to default 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>

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./proxy.php?url=http%3A%2F%2Fexample%2Fajax%3Fid%3D123 gets the data from http://example/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./proxy.php?id=123 to access http://example/ajax?id=123.

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741984699a4377098.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信