jQuery的AJAX只读取头和据此决定是否获取内容

jQuery的AJAX只读取头和据此决定是否获取内容

本文介绍了jQuery的AJAX只读取头和据此决定是否获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做)的基础上 jQuery.ajax(一个AJAX脚本并且已经到了一个地步,我应该以某种方式检查,如果我想的链接加载是一个HTML页面或不同的东西就像一个SWF文件,图像或拉链。所以,我需要以某种方式检查Content-Type头,并决定是否我应该得到的内容也(如果是HTML)或引发Ajax调用的路程,做一个了window.location = theUrl 。我不希望是让整个文件只是为了找出它的100MB的zip文件。

I'm making an AJAX script based on jQuery.ajax() and have come to a point where I should somehow check if the link I'm trying to load is a html page or something different like a swf, image or zip. So I need to somehow check the content-type header and decide if I should get the content also (if it's html) or throw the ajax call away and do a window.location = theUrl. What I don't want is to get the entire file just to find out it's a 100MB zip file.

有没有一种方法,以暂停(或放弃)的要求,同时它仍在继续和刚读头? HEAD 呼叫不是一种选择,因为这样我就必须尽一切时间2请求到服务器。

Is there a way to 'pause' (or abort) the request while its still going and read just the headers ? HEAD call is not an option because this way I would have to make 2 requests to the server every time.

也许有的用的setTimeout 低级别排序黑客和XHR功能?

Maybe some sort of hack with setTimeout and low-level xhr functions?

在此先感谢! :)

编辑:我试着去从XHR标头中的setTimeout请求完成之前,但它似乎不来填充它,直到所有的数据被取出

Tried to get the headers from xhr in setTimeout before the request finished but it doesn't seem to populate it until all of the data has been fetched.

编辑2:我砍死我周围的jQuery的东西的方式,结合自身的onreadystatechange:

EDIT 2: I hacked my way around the jquery's thing that binds itself to onreadystatechange:

var xhrr = new window.XMLHttpRequest();
$.ajaxSetup({
    xhr: function() { return xhrr }
});

...
$.ajax(....);

var theirfunc = xhrr.onreadystatechange;
xhrr.onreadystatechange = function() {
    console.log('xhr state: ', xhrr.readyState);
    theirfunc();
};

因此​​,这给了我州1,2,3,4,为了和我能得到的内容类型和中止成功。我仍然在调查为什么当jQuery的本身创建XMLHtt prequest对象将无法正常工作。如果我跳过ajaxSetup部分而 VAR xhrr = $。阿贾克斯(...)然后绑定它不会以同样的方式。因此,如何是我XHR比jQuery的有什么不同?我认为他们这样做是这样的:

So this gave me states 1, 2, 3, 4 in order and I could get the content-type and abort successfully. I'm still investigating why it won't work when jquery itself creates the XMLHttpRequest object. If I skip the ajaxSetup part and get var xhrr = $.ajax(...) then bind the same way it won't work. So how is my xhr any different than jquery's? I see they do it like this:

function createStandardXHR() {
    try {
        return new window.XMLHttpRequest();
    } catch( e ) {}
}

所以它不应该有所作为?

So it shouldn't make a difference?

编辑3:找到了! jQuery的1.6返回假XHR对象只是少数的属性和的onreadystatechange 就是其中的一个也没有。

EDIT 3: Found it! jquery 1.6 returns a fake xhr object with just a small number of the properties and onreadystatechange is NOT one of them.

推荐答案

您可能需要破解的jQuery。我不知道如何做到这一点的细节,但是...

You might have to hack jQuery. I don't know how to do this in detail, but...

jQuery使用XMLHtt prequest()来加载数据。如果你看这里(头脑那里的页面滚动)

jQuery uses XMLHttpRequest() to load data. If you look here (mind where the page will be scrolled)

你会发现它的某种方式可以附加一个监听器监听要加载的标题。该数据随后将继续加载,但使用中止()函数,则可以中止该。如果你看一下jQuery的源$ C ​​$ C:

you will find that it's somehow possible to attach a listener that listens for the headers to be loaded. The data would then continue to load, but using the abort() function you can abort that.If you look in the jQuery source code:

<一个href="http://www.google.com/$c$csearch#LARMQtWqu54/trunk/spec/support/jquery-1.4.4.js&q=xmlhtt$p$pquest%20package:jQuery&type=cs" rel="nofollow">http://www.google.com/$c$csearch#LARMQtWqu54/trunk/spec/support/jquery-1.4.4.js&q=xmlhtt$p$pquest%20package:jQuery&type=cs

和搜索xhr.send(1场)和window.XMLHtt prequest(2场),你会看到这些选项不使用jQuery的本身(注意XHR是一个XMLHtt prequest) 。然而,在window.XMLHtt prequest第一场比赛就显示在你可以覆盖这个对象的意见。然而,在第二场比赛的评论是不存在的,所以我不知道这是否会,以及覆盖(因此你可能需要破解有它在所有浏览器)。您需要延长XMLHtt prequest和修改的构造和/或发送方法调用其父。传递对象的jQuery的方式有意见参考。

and search for xhr.send (1 match) and window.XMLHttpRequest (2 matches) , you will see that those options are not used by jQuery natively (note that xhr is an XMLHttpRequest). However, in the first match of window.XMLHttpRequest it is indicated in the comments that you can override this object. However, in the second match the comment is not there, so I'm not sure if this would be overridden as well (hence you might need to hack to have it work in all browsers). You would need to extend XMLHttpRequest and modify the constructor and/or send method to call its parent's. Pass that object to jQuery in the way the comments refer to.

我希望你知道如何监听工作和如何面向对象的工作在JavaScript。

I hope you know how listeners work and how object-orientation works in javascript.

这篇关于jQuery的AJAX只读取头和据此决定是否获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 02:00