jQuery ajax() 选项 - xhr
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1644418/
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
jQuery ajax() option - xhr
提问by Vaclav Kohout
In jQuery ajax function there is xhr option. Does someone know more details, usability or sample usage of this option?
在 jQuery ajax 函数中有 xhr 选项。有人知道此选项的更多详细信息、可用性或示例用法吗?
采纳答案by Matt Ball
In fact, someone does know.
事实上,确实有人知道。
The xhr
option allows you to define your own callback for creating the XMLHttpRequest object that will be used behind the scenes in the ajax()
call. In pretty much every single case, you shouldn't need to specify this option.
该xhr
选项允许您定义自己的回调,以创建将在ajax()
调用的幕后使用的 XMLHttpRequest 对象。几乎在每种情况下,您都不需要指定此选项。
回答by sujal
For this function, you want to return an appropriate XHR object for your browser. The default behavior is to use XMLHTTPRequest or the IE equivalent. Here's the default behavior:
对于此函数,您希望为浏览器返回适当的 XHR 对象。默认行为是使用 XMLHTTPRequest 或等效的 IE。这是默认行为:
jQuery.ajaxSettings.xhr = window.ActiveXObject ?
/* Microsoft failed to properly
* implement the XMLHttpRequest in IE7 (can't request local files),
* so we use the ActiveXObject when it is available
* Additionally XMLHttpRequest can be disabled in IE7/IE8 so
* we need a fallback.
*/
function() {
return !this.isLocal && createStandardXHR() || createActiveXHR();
} :
// For all other browsers, use the standard XMLHttpRequest object
createStandardXHR;
Those two create methods createStandardXHR
and createActiveXHR
essentially call the basic XHR creation methods we've all known and loved for years. Here's createStandardXHR:
这两个创建方法createStandardXHR
和createActiveXHR
基本拨打我们都熟知和喜爱多年的基本XHR创建方法。这是 createStandardXHR:
function createStandardXHR() {
try {
return new window.XMLHttpRequest();
} catch( e ) {}
}
So, if you wanted to override this, you can simply pass in your own function that returns a new XMLHttpRequest()
object.
所以,如果你想覆盖它,你可以简单地传入你自己的返回一个new XMLHttpRequest()
对象的函数。
Why would you want to do this? Let's say you need to make a cross domain HTTP request and are using an IFRAME shim to make it work using document.domain
to work within the same origin rules. This is a good way to make your javascript load the XHR object from the correct frame based on what domain you want to talk to.
你为什么想做这个?假设您需要发出一个跨域 HTTP 请求,并使用 IFRAME 垫片使其document.domain
在相同的源规则内工作。这是让您的 javascript 根据您要与之对话的域从正确的框架加载 XHR 对象的好方法。
Twitter.com uses this technique.
Twitter.com 使用这种技术。
JavaScript runs on http://twitter.com/but the data is at http://api.twitter.com. They create an IFRAME pointing at api.twitter.com that simply sets document.domain
to "twitter.com"
. They set document.domain
to "twitter.com"
in the main page, too.
JavaScript 在http://twitter.com/ 上运行,但数据在http://api.twitter.com 上。他们创建了一个指向 api.twitter.com 的 IFRAME,它只是设置document.domain
为"twitter.com"
. 他们设置document.domain
到"twitter.com"
主网页了。
Then, their JS, when making HTTP requests, just creates it from the IFRAME instead of the main page. Gets them through the same-origin policy.
然后,他们的 JS 在发出 HTTP 请求时,只是从 IFRAME 而不是主页面创建它。通过同源策略获取它们。
You can do this with the xhr option to $.ajax(). Here's a snippet (imagine this code running on a page at http://myapp.com):
您可以使用 $.ajax() 的 xhr 选项来执行此操作。这是一个片段(想象一下这段代码在http://myapp.com的页面上运行):
$.ajax({url: "http://api.myapp.com", xhr: function(){
return new ($('#my_api_iframe')[0].contentWindow.XMLHttpRequest)();
}, success: function(html) {
// format and output result
}
});
That will work as long as both the main page and the iframe set their document.domain
to the same value. (This is a hackish example: it won't work in some IE versions because I cheated and only used the standard XMLHttpRequest object - you'll need to fix.)
只要主页和 iframe 将它们设置document.domain
为相同的值,这就会起作用。(这是一个骇人听闻的例子:它在某些 IE 版本中不起作用,因为我作弊并且只使用了标准的 XMLHttpRequest 对象 - 您需要修复。)
Hope that helps.
希望有帮助。
(edited to add: this is a technique necessary for older browsers - CORS support in most modern browsers would make this unnecessary)
(编辑添加:这是旧浏览器所必需的技术 - 大多数现代浏览器中的 CORS 支持将使其变得不必要)
Sujal
苏加尔
回答by Sneaky Wombat
Another late answer, but the code below is a great example of what you can do when you override the xhr option. This is taken from Ben Nolan's blogIt allows you to track the progress of an xhr get. I used this to create a progress bar when loading a large json data set, exactly the same thing he created it for. It helped me a ton.
另一个迟到的答案,但下面的代码是一个很好的例子,说明当你覆盖 xhr 选项时你可以做什么。 这是摘自 Ben Nolan 的博客,它允许您跟踪 xhr get 的进度。我在加载大型 json 数据集时使用它来创建进度条,与他创建它的目的完全相同。它帮了我很多忙。
interval = null
$.ajax {
url : "endpoint.json"
dataType : 'json'
xhr : () =>
xhr = jQuery.ajaxSettings.xhr()
interval = setInterval( =>
if xhr.readyState > 2
total = parseInt(xhr.getResponseHeader('Content-length'))
completed = parseInt(xhr.responseText.length)
percentage = (100.0 / total * completed).toFixed(2)
console.log "Completed #{percentage}%"
, 50)
xhr
complete: ->
clearInterval(interval)
success : (data) =>
alert(data)
}
回答by Bartek
http://api.jquery.com/is your friend. From that site when you search for the ajax() command:
http://api.jquery.com/是你的朋友。从该站点搜索 ajax() 命令时:
xhr (Function): Callback for creating the XMLHttpRequest object. Defaults to the ActiveXObject when available (IE), the XMLHttpRequest otherwise. Override to provide your own implementation for XMLHttpRequest or enhancements to the factory.It's not available in jQuery 1.2.6 and in any early version.
xhr (Function):用于创建 XMLHttpRequest 对象的回调。可用时默认为 ActiveXObject (IE),否则为 XMLHttpRequest。覆盖以提供您自己的 XMLHttpRequest 实现或对工厂的增强。它在 jQuery 1.2.6 和任何早期版本中不可用。
Generally, unless you know what you're doing you really won't be using this function of the $.ajax() function
一般来说,除非你知道你在做什么,否则你真的不会使用 $.ajax() 函数的这个函数