开发者

AJAX call to different domain without JSONP

Is there any way to retrieve data via ajax from another domain without 开发者_Python百科using JSONP or any other library (a pure JS solution?)


Try to look at CORS, however it's not supported by every browser.


JSONP is not a library, it's a technique. JSONP itself isn't related to or dependent upon jQuery or anything like that, though jQuery does have support that makes it simple. You can easily do this with plain DOM/JavaScript.

The technique is to dynamically insert a script tag into the document, setting the source to your external server. The external server should send back JSON data wrapped in a function. The function is called when the script is loaded, of course.

So, say you have a function in your main script, json_parse.

Insert a tag like

<script src='yourserver.example.com/the_data.php?jsonp=json_parse'></script>

And let's say that script sends back the data, wrapped in a function call to the function you specify, json_parse in this case.

json_parse({"some":"data","blap":420});

And there you go, you've accessed JSON data from an XD server. Note since you're loading a script, this only works for GET requests.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜