最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I can't change HTTP request header Content-Type value using jQuery - Stack Overflow

programmeradmin1浏览0评论

I tried to override HTTP request header content by using jQuery's AJAX function. It looks like this

$.ajax({
  type : "POST",
  url : url,
  data : data,
  contentType: "application/x-www-form-urlencoded;charset=big5",
  beforeSend: function(xhr) {
      xhr.setRequestHeader("Accept-Charset","big5");
      xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=big5");
  },
  success: function(rs) {
    target.html(rs);
  }
});

Content-Type header is default to "application/x-www-form-urlencoded; charset=UTF-8", but it obviously I can't override its value no matter I use 'contentType' or 'beforeSend' approaches. Could anyone adivse me a hint that how do I or can I change the HTTP request's content-type value? thanks a lot.

btw, is there any good documentation that I can study JavaScript's XMLHttpRequest's encoding handling?

I tried to override HTTP request header content by using jQuery's AJAX function. It looks like this

$.ajax({
  type : "POST",
  url : url,
  data : data,
  contentType: "application/x-www-form-urlencoded;charset=big5",
  beforeSend: function(xhr) {
      xhr.setRequestHeader("Accept-Charset","big5");
      xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=big5");
  },
  success: function(rs) {
    target.html(rs);
  }
});

Content-Type header is default to "application/x-www-form-urlencoded; charset=UTF-8", but it obviously I can't override its value no matter I use 'contentType' or 'beforeSend' approaches. Could anyone adivse me a hint that how do I or can I change the HTTP request's content-type value? thanks a lot.

btw, is there any good documentation that I can study JavaScript's XMLHttpRequest's encoding handling?

Share Improve this question asked Oct 30, 2009 at 4:53 MattMatt 1,7015 gold badges23 silver badges36 bronze badges 3
  • See stackoverflow./questions/1145588/…, a duplicate question to this one. – Jim Ferrans Commented Oct 30, 2009 at 5:23
  • @Jim: not really; that other Q is about the Accept header, not about setting the content-type. – Martijn Pieters Commented May 7, 2011 at 22:37
  • From the jQuery ajax documentation: Data will always be transmitted to the server using UTF-8 charset; you must decode this appropriately on the server side. api.jquery./jQuery.ajax – Bkress Commented May 20, 2011 at 20:50
Add a ment  | 

2 Answers 2

Reset to default 2

According to the jQuery docs, charset will always be UTF-8: http://jqapi./#p=jQuery.ajax. I guess the same goes for accept-charset.

Big5 is a subset of UTF-8, right? So it should be possible to encode and decode it server-side.

Have you checked the w3 documentation? There appears to be some limitation in using Accept-Charset header. http://www.w3/TR/XMLHttpRequest/#the-setrequestheader-method

发布评论

评论列表(0)

  1. 暂无评论