jQuery如何做它的跨域ajax调用,如何用mootools复制em [英] How do jQuery do its cross-domain ajax calls and how can I replicate em with mootools
问题描述
在我永恒的内部斗争是否留在mootools或跳转到jQuery我已经找到在jQuery文档的东西,引起我的注意,这是jQuery可以要求一个JSON到不同的域,这通常被禁止
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.
我已经看到跨子域的一些解决方法,但从来没有跨域,我真的很兴奋,首先我认为我是服务器相关的但实验一点点我已经看到,做同样的JSON请求从jQuery文档Mootools不起作用!
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:
This works 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;
});
});
这不是
Mootools:
This doesn't 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文档: http: //docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback
Mootols Doc: http://mootools.net/docs/Request/Request.JSON
jQuery Doc: http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback Mootols Doc: http://mootools.net/docs/Request/Request.JSON
推荐答案
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:
{foo:bar,baz:bah}
它会返回:
temporaryCallbackFunctionName({foo:bar,baz:bah});
jQuery定义了临时回调函数,并插入< script src =...
jQuery defines the temporary callback function and inserts a <script src="..."></script>
element, which is not limited by the same origin policy.
当脚本被加载时,函数
缺点是,如果服务器是恶意(或黑客),它现在可以在浏览器中执行任意代码。
The downside is that if the server is evil (or hacked) it can now execute arbitrary code in your browser.
这篇关于jQuery如何做它的跨域ajax调用,如何用mootools复制em的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!