jQuery如何进行跨域ajax调用以及如何使用mootools复制em

时间:2022-08-23 10:24:24

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#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

4 个解决方案

#1


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定义临时回调函数并插入

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.

更多信息在这里。

#2


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制作。

#3


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 page for more info.

有关详细信息,请查看此文档页面。

#4


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.

这是有限的原因可能是因为跨站点脚本攻击。

#1


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定义临时回调函数并插入

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.

更多信息在这里。

#2


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制作。

#3


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 page for more info.

有关详细信息,请查看此文档页面。

#4


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.

这是有限的原因可能是因为跨站点脚本攻击。