jQuery 如何进行跨域 ajax 调用以及如何使用 mootools 复制它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/726704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do jQuery do its cross-domain ajax calls and how can I replicate em with mootools
提问by perrohunter
in my eternal internal fight on whether to stay with mootools or jump to jQuery I've found on the jQuery documentation something that got my attention and this is that jQuery can ask for a JSON to a different domain, which is usually forbidden by the browser.
在我关于是继续使用 mootools 还是跳转到 jQuery 的永恒内部斗争中,我在 jQuery 文档中发现了一些引起我注意的东西,这是 jQuery 可以向不同的域请求 JSON,这通常被浏览器禁止.
I've seen some workarounds for cross-subdomain, but never cross-domain, and I'm really thrilled, first I thought I was server related but experimenting a little bit more I've seend that doing the very same JSON request from jQuery docs on Mootools doesn't work!
我已经看到了一些跨子域的解决方法,但从来没有跨域,我真的很激动,首先我以为我是服务器相关的,但我做了更多的实验,我看到了从 jQuery 执行完全相同的 JSON 请求Mootools 上的文档不起作用!
This works jQuery:
这适用于 jQuery:
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
function(data){
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images");
if ( i == 3 ) return false;
});
});
This doesn't Mootools:
这不是 Mootools:
var jsonRequest = new Request.JSON({url: "http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", onComplete: function(person, responseText){
alert(responseText);
}}).get({});
How can I replicate this behavior ? what causes it ?
我怎样才能复制这种行为?是什么原因造成的?
jQuery Doc: http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallbackMootols Doc: http://mootools.net/docs/Request/Request.JSON
jQuery 文档:http: //docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback Mootols 文档:http: //mootools.net/docs/Request/Request.JSON
回答by Jaka Jan?ar
It's said right on the page that it's JSONP.
页面上说它是JSONP。
JSONP is a trick where the server, instead of returning the usual response, wraps it into a method call of the user-supplied method, e.g. instead of:
JSONP 是一种技巧,其中服务器不返回通常的响应,而是将其包装到用户提供的方法的方法调用中,例如,而不是:
{"foo": "bar", "baz":"bah"}
{"foo": "bar", "baz":"bah"}
It would return:
它会返回:
temporaryCallbackFunctionName({"foo": "bar", "baz":"bah"});
temporaryCallbackFunctionName({"foo": "bar", "baz":"bah"});
jQuery defines the temporary callback function and inserts a <script src="..."></script>
element, which is not limited by the same origin policy.
jQuery 定义了临时回调函数并插入<script src="..."></script>
元素,不受同源策略限制。
When the script is loaded, the function is executed and that's it.
当脚本被加载时,函数被执行,就是这样。
The downside is that if the server is evil (or hacked) it can now execute arbitrary code in your browser.
缺点是,如果服务器是邪恶的(或被黑客入侵),它现在可以在您的浏览器中执行任意代码。
More info here.
更多信息在这里。
回答by moff
You may use JSONP in MooTools by using a plugin, JSONP. It's made by Aaron Newton, one of the core MooTools developers.
您可以通过插件 JSONP 在 MooTools 中使用JSONP。它由MooTools 的核心开发人员之一Aaron Newton开发。
回答by moff
This is included in MooTools more since v1.2.2 (released on April 23rd 2009).
自 v1.2.2(2009 年 4 月 23 日发布)以来,它更多地包含在 MooTools 中。
Check this documentation pagefor more info.
查看此文档页面以获取更多信息。
回答by Seb
It seems you can't do it with Mootools, according to its API docs and this forum.
根据其 API 文档和本论坛,您似乎无法使用 Mootools 做到这一点。
The reason this is limited is probably because of Cross-site scripting attacks.
之所以受限,可能是因为跨站脚本攻击。