JavaScript, JSONP and reading XML from cross-domain

前端 未结 4 1511
离开以前
离开以前 2020-12-15 01:58

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 ne

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 02:43

    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('')
    

    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.

提交回复
热议问题