firefox XMLHttpRequest post提交数据数据content-type自动增加charset=utf-8

  firefox使用XMLHttpRequest对象post提交数据的时候,不管你是否设置过RequestHeader的content-type,firefox都会自动增加content-type,并且最后增加charset=utf-8的请求头。

  网上找了下资料,据说是firefox的bug,一直没有修正。有些服务器如果碰到不需要的charset=utf-8之类的,会导致无法识别而报错。


https://bugzilla.mozilla.org/show_bug.cgi?id=416178
http://support.mozilla.org/zh-CN/questions/741629



  上面2个帖子都是关于firefox XMLHttpRequest对象post数据时,出现了自动给content-type增加charset=utf-8导致出现问题。

  如果你的服务器确实无法识别多余的content-type charset=utf-8内容,而且有大量数据需要提交,只能用普通表单的post提交到隐藏的iframe来实现无刷新了。

  如果是小量数据,可以使用get提交,这样不会设置content-type。即使设置了content-type,也不会增加多余的charset=utf-8内容。

  测试示例和截图对比
  
  

var xhr = new XMLHttpRequest();
    xhr.open("post", "1.asp", false);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //如果不发送数据 xhr.send(null),也不会增加多余的charset=utf-8
    //注意是xhr.send(null),不能xhr.send(''),这样也会添加charset=utf-8
    xhr.send("a=b");
    alert(xhr.responseText)

firebug监视

firefox XMLHttpRequest post提交数据数据content-type自动增加charset=utf-8

chrome监视

firefox XMLHttpRequest post提交数据数据content-type自动增加charset=utf-8

  即使去掉xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");这句,当post请求发送了数据的时候,firefox还是会自动设置content-type为text/plain; charset=UTF-8
firefox XMLHttpRequest post提交数据数据content-type自动增加charset=utf-8

加支付宝好友偷能量挖...


原创文章,转载请注明出处:firefox XMLHttpRequest post提交数据数据content-type自动增加charset=utf-8

评论(0)Web开发网
阅读(540)喜欢(0)操作系统及应用软件