jQuery.ajax与POST或PUT没有响应头为IE8和IE9 [英] jQuery.ajax with POST or PUT has no response headers for IE8 and IE9

查看:2118
本文介绍了jQuery.ajax与POST或PUT没有响应头为IE8和IE9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,与IE8和IE9,如果我叫我使用jQuery.ajax()与REST的API 发表 PUT 动词,那么我不取回任何响应头在jqXHR。然而,GET请求正常工作。

此行​​为与其他浏览器不同。我验证过铬,FF,Opera和Safari都返回的全套预期的报头中的POST和PUT请求的响应。只有IE8和IE9似乎被抛头在地板上。 (有一件事我没有检查是HEAD请求会发生什么。)

我已经与验证提琴手的标题实际上使其通过线路,所以这个问题是无论是与jQuery本身或与IE8和IE9。

这是一个已知的问题?如果是这样,有没有解决方法。我可以重载以下POST和PUT /覆盖东西了jQuery preserve头?我现在的解决方法是使用成功的回调函数里面一个GET简单地重新读取该修改的数据,因为IE8和IE9不要乱用头文件获取操作。

下面是我的主要基于jQuery的AJAX辅助方法的一个片段:

  $。阿贾克斯({
        网址:的String.Format(um.proxy.url,网址)
        类型:ajaxParams.verb,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        数据:的String.Format({0},ajaxParams.jsonData)
        标题:mapOfHeaders,
        成功:功能(数据,textStatus,jqXHR){
            // ...
        },
        错误:函数(MSG,textStatus,errorThrown){
            // ...
        }
    });
 

解决方案

事实证明,这是一个已知问题与IE浏览器的所有当前的味道(7,8,9)。如果响应code是 204 ,然后IE浏览器简单地抛出地板上的标题。对于像我这样的人编写需要一个上最新的eTag并发检查,纯AJAX实现,IE杀害纯AJAX方法,迫使我做即时GET获取当前的eTag。 :facepalm:

下面有一对夫妇的文章中,我成功地挖了起来:

ETag头在HTTP在Internet Explorer中没有内容的响应缺少

http://dalelane.co.uk/blog/?p=2043

response.headers.ETag未定义

<一个href="http://datajs.$c$cplex.com/discussions/398295">http://datajs.$c$cplex.com/discussions/398295

一个部分解决办法是闻的IE浏览器,然后做一个直接的HEAD请求来获取ETag的,但是这种方法的问题是,如果有人设法偷偷在你前面,并更新记录,你会得到的eTag为的的变化,而不是的的。因此,这不是一个可以接受的解决办法。

所以,我嗅出了IE浏览器,做一个直接的GET。否则,对于所有其他浏览器,我只是使用jQuery的jqXHR.getResponseHeader(namespace.constants.headers.eTag)获得的eTag。

I've noticed that with IE8 and IE9, if I call my RESTful API using jQuery.ajax() with POST and PUT verbs, then I don't get back any response headers in jqXHR. However, GET requests work as expected.

This behavior is different from all other browsers. I've verified that Chrome, FF, Opera, and Safari all return the full set of expected headers in the response for POST and PUT requests. Only IE8 and IE9 seem to be throwing the headers on the floor. (One thing I haven't checked is what happens with HEAD requests.)

I've verified with Fiddler that the headers are actually making it over the wire, so the problem is either with jQuery itself or with IE8 and IE9.

Is this a known issue? If so, is there a workaround. Can I overload/overwrite something in jQuery to preserve headers following POST and PUT? My current workaround is to simply refetch the modified data using a GET inside the success callback since IE8 and IE9 don't mess with headers for GET operations.

Here's a snippet of my main jQuery-based AJAX worker method:

    $.ajax({
        url: String.format(um.proxy.url, url),
        type: ajaxParams.verb,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: String.format('{0}', ajaxParams.jsonData),
        headers: mapOfHeaders,
        success: function (data, textStatus, jqXHR) {
            //...
        },
        error: function (msg, textStatus, errorThrown) {
            //...
        }
    });

解决方案

Turns out that this is a known issue with all current flavors of IE (7, 8, and 9). If the response code is a 204, then IE simply throws all headers on the floor. For folks like me writing pure AJAX implementations that require an up-to-date eTag for concurrency checks, IE slays the pure AJAX approach and forces me to do an immediate GET to fetch the current eTag. :facepalm:

Here are a couple of articles I managed to dig up:

ETag header missing in HTTP No Content responses in Internet Explorer

http://dalelane.co.uk/blog/?p=2043

response.headers.ETag undefined

http://datajs.codeplex.com/discussions/398295

One partial workaround is to sniff for IE and then do an immediate HEAD request to fetch the ETag, but the problem with this approach is that if someone has managed to sneak in ahead of you and update the record, you'll be getting the eTag for her changes, not yours. So this is not an acceptable workaround.

So, I sniff for IE and do an immediate GET. Otherwise for all other browsers, I simply use jQuery's jqXHR.getResponseHeader(namespace.constants.headers.eTag) to get the eTag.

这篇关于jQuery.ajax与POST或PUT没有响应头为IE8和IE9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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