在IE中,jquery的“X-Requested-With”标题变为“x-requested-with”(小写)

时间:2009-07-15 09:16:52

标签: ajax internet-explorer jquery http-headers

为什么要转换为小写? 在Firefox中它是:“X-Requested-With”。 在IE中,它是:“x-requested-with”

2 个答案:

答案 0 :(得分:15)

HTTP方法应该区分大小写,但根据RFC 2616,HTTP标头应该不区分大小写。

答案 1 :(得分:9)

我注意到了类似的东西。看一下示例代码以及添加一些自定义HTTP标头时的功能。首先是JavaScript代码,然后是来自IE8,Safari4和Firefox3的Fiddler转储(仅限自定义标头)。请注意,Firefox尊重大小写,IE转换为小写,Safari转换为大小写。

但是,如前所述,服务器将这些视为不区分大小写,因此无关紧要。

function doXHR() {
  var request = new XMLHttpRequest();
  request.open('GET', '/header/header.txt');
  request.setRequestHeader('x-lowercase', 'X-lowercase');
  request.setRequestHeader('x-Propercase', 'X-Propercase');
  request.setRequestHeader('x-CamelCase', 'X-CamelCase');
  request.setRequestHeader('x-UPPERCASE', 'X-UPPERCASE');
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      console.log('Received XMLHttpRequest callback: \n' + request.responseText);
    }
  };
  request.send("");
}

User-Agent:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 6.0; Trident / 4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618)

x-lowercase: X-lowercase
x-camelcase: X-CamelCase
x-uppercase: X-UPPERCASE
x-propercase: X-Propercase

User-Agent:Mozilla / 5.0(Windows; U; Windows NT 6.0; en-US)AppleWebKit / 528.18(KHTML,类似Gecko)版本/ 4.0 Safari / 528.17

X-Lowercase: X-lowercase
X-Uppercase: X-UPPERCASE
X-Camelcase: X-CamelCase
X-Propercase: X-Propercase

User-Agent:Mozilla / 5.0(Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6)Gecko / 2009011913 Firefox / 3.0.6(.NET CLR 3.5.30729)

x-lowercase: X-lowercase
x-Propercase: X-Propercase
x-CamelCase: X-CamelCase
x-UPPERCASE: X-UPPERCASE