jQuery的AJAX跨域财产的用法? [英] Usages of jQuery's ajax crossDomain property?

查看:156
本文介绍了jQuery的AJAX跨域财产的用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据jQuery的:

  

跨域(默认值:假的同域的请求,适用于   跨域请求)类型:Boolean如果要强制执行   跨域请求(如JSONP)在同一个域中,设置的值   跨域的为true。这允许,例如,服务器端   重定向到另一个域。 (版新增:1.5)

我不明白上面。

如果在code是

  $(文件)。就绪(函数()
{
    $阿贾克斯(
    {
        网址:http://es3.com/Handlers/MyHandler.ashx,
        缓存:假的,
        数据类型:JSONP
        ...
        ...
    });
});

功能AAA(JSON)
{
    警报(json.result);
}
 

和IM specifiying 数据类型:JSONP ,那么响应将是应用程序/ JavaScript的MIME类型,监守这将在浏览器中运行的脚本。

我不认为有任何理由为什么它不会像那个当我在同一个站点运行此code。 (所以 - 我不明白的用法该属性)

我已经做了一个样本

我有2个(主调整)域。 es2.com es3.com

(注意,网址中的code是始终为 es3.com

测试#1:

运行由 es3.com 的code:(左窗格)
运行由 es2.com 的code:(右窗格)
跨域:假(默认情况下丢失时)

看区别:( http://i.stack.imgur.com/RKyZp。 JPG

输入图像的描述在这里

测试2:

运行由 es3.com 的code:(左窗格)
运行由 es2.com 的code:(右窗格)
跨域:真正的< ---通知

http://i.stack.imgur.com/xEcyd.jpg

我看不出有什么区别。

问:

为什么/当我需要设置跨域属性?

解决方案

默认值跨域 如下:

  

假的同域的请求,如此跨域请求

数据类型是跨preTED不同,具体取决于为跨域设定值:

  

JSON:评估响应的JSON,并返回一个JavaScript   目的。跨域的JSON请求转换为JSONP,除非   请求包括JSONP:假的在请求的选项

由于您使用的是 JSONP 而不是 JSON ,你不会看到你的测试有什么区别。

  

当我需要的跨域属性设置?

如果您进行的是同一个域 JSON 要求的的网站可能会请求重定向到另一个域名服务响应(通过HTTP 3XX),那么你应该将跨域属性设置为true,以便响应可以通过你的调用脚本读取。

这让你做同样的原产地请求时检索JSON的优势,使得跨域请求时JSONP的功能。如果CORS是活跃的领域,你重定向到,那么你可以设置 JSONP:在请求选项错误

示例

制作从example.com的请求,将example.org。

  • 跨域自动设置为true。
  • 设置为 JSONP
  • 数据类型。

结果:按example.org返回JSONP

制作从example.com请求example.com。

  • 跨域自动设置为false。
  • 设置为 JSONP
  • 数据类型。

结果:按example.com返回JSONP

制作从example.com的请求,将example.org。

  • 跨域自动设置为true。
  • 设置为 JSON
  • 数据类型。

结果:按example.org返回JSONP

制作从example.com请求example.com。

  • 跨域自动设置为false。
  • 设置为 JSON
  • 数据类型。

结果:按example.com返回的JSON

制作从example.com的请求,将example.org。

  • 跨域自动设置为true。
  • 设置为 JSON
  • 数据类型。
  • JSONP 设置为false。
  • 在example.org不支持CORS为example.com

结果:按浏览器的返回CORS误差

制作从example.com请求example.com,example.com重定向AJAX来example.edu。

  • 跨域手动设置为true。
  • 设置为 JSON
  • 数据类型。

结果:按example.edu返回JSONP

制作从example.com的请求,将example.org。

  • 跨域自动设置为true。
  • 设置为 JSON
  • 数据类型。
  • JSONP 设置为false。
  • 在example.org不支持CORS为example.com

结果:按example.org返回的JSON

制作从example.com请求example.com,example.com重定向AJAX来example.edu。

  • 跨域自动设置为false。
  • 设置为 JSON
  • 数据类型。
  • 在example.edu不支持CORS为example.com

结果:按浏览器的返回CORS误差

According to jQuery :

crossDomain (default: false for same-domain requests, true for cross-domain requests) Type: Boolean If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. (version added: 1.5)

I don't understand the above.

If the code is

$(document).ready(function ()
{
    $.ajax(
    {
        url: 'http://es3.com/Handlers/MyHandler.ashx',
        cache: false,
        dataType: "jsonp",
        ...
        ...
    });
});

function aaa(json)
{
    alert(json.result);
}

And im specifiying datatype:jsonp, then the response is going to be application/javascript mime typed , becuase it's a script which will run in my browser.

I dont see any reason why it would not act like that when I'm running this code under the same domain. ( hence - I don't see the usages for this property).

I have made a sample

I have 2 (host tweaked) domains. es2.com and es3.com.

(notice , the url in the code is always to es3.com)

Test #1 :

Run the code from es3.com : (left pane)
Run the code from es2.com : (right pane)
crossDomain:false (default when missing).

look at the differences : (http://i.stack.imgur.com/RKyZp.jpg)

Test #2 :

Run the code from es3.com : (left pane)
Run the code from es2.com : (right pane)
crossDomain:true <--- notice

(http://i.stack.imgur.com/xEcyd.jpg)

I don't see any difference.

Question :

Why / When do I need to set the crossDomain property ?

解决方案

The default for crossDomain is as follows:

false for same-domain requests, true for crossDomain requests

The data-type is interpreted differently depending on the value for the crossDomain setting:

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options

Because you are using jsonp instead of json you won't see any difference in your tests.

When do I need to set the crossDomain property ?

If you are making a same domain json request, and your site may redirect the request to another domain to serve the response (via HTTP 3XX), then you should set the crossDomain property to true so the response can be read by your calling script.

This gives you the advantage of retrieving JSON when making same origin requests, and the functionality of JSONP when making cross-origin requests. If CORS is active on the domain you redirect to then you can set jsonp: false in the request options.

Examples

Making a request from example.com to example.org.

  • crossDomain automatically set to true.
  • Data type set to jsonp.

Result: JSONP returned by example.org.

Making a request from example.com to example.com.

  • crossDomain automatically set to false.
  • Data type set to jsonp.

Result: JSONP returned by example.com.

Making a request from example.com to example.org.

  • crossDomain automatically set to true.
  • Data type set to json.

Result: JSONP returned by example.org.

Making a request from example.com to example.com.

  • crossDomain automatically set to false.
  • Data type set to json.

Result: JSON returned by example.com.

Making a request from example.com to example.org.

  • crossDomain automatically set to true.
  • Data type set to json.
  • jsonp is set to false.
  • example.org does not support CORS for example.com

Result: CORS error returned by browser.

Making a request from example.com to example.com, example.com redirects AJAX to example.edu.

  • crossDomain manually set to true.
  • Data type set to json.

Result: JSONP returned by example.edu.

Making a request from example.com to example.org.

  • crossDomain automatically set to true.
  • Data type set to json.
  • jsonp is set to false.
  • example.org does support CORS for example.com

Result: JSON returned by example.org.

Making a request from example.com to example.com, example.com redirects AJAX to example.edu.

  • crossDomain automatically set to false.
  • Data type set to json.
  • example.edu does not support CORS for example.com

Result: CORS error returned by browser.

这篇关于jQuery的AJAX跨域财产的用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆