V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
sunhk25
V2EX  ›  问与答

ajax 传输数据的编码问题

  •  
  •   sunhk25 · Mar 1, 2018 · 2121 views
    This topic created in 2992 days ago, the information mentioned may be changed or developed.
    • 页面不是 utf-8 时,即使指定编码方式,POST 方法传输的还是 utf-8 编码的转义字符, data:key=123&value=日本語=>key=123&value=%E6%97%A5%E6%9C%AC%E8%AA%9E%
    • 但是 url 后的参数却没有问题,按指定的 shift-jis 进行的编码转义, url:getInfo?tstParam=日本語=>getInfo?tstParam=%93%FA%96{%8C%EA

    想问一下

    • 数据转码跟服务器的设置没有关系对不对。
    • post 的数据只能按 encodeURIComponent 的 utf-8 的编码转义吗?
    $.ajax({
        contentType: "charset=Shift_JIS",
        //contentType : "application/x-www-form-urlencoded; charset=UTF-8",
        // beforeSend : function(xhr) {
        //   xhr.overrideMimeType("text/plain; charset=shift_jis");
        // },
          url: 'getInfo.php?tstParam=日本語',
          data : {key: "123", value:"日本語"},
          type: "POST",
          dataType: 'json',
          timeout:10000,
      }).done(function(data) {
          console.log("OK");
      }).fail(function(XMLHttpRequest, textStatus, errorThrown) {
          console.log("ERR");
      })
    
    2 replies    2018-03-01 10:52:16 +08:00
    BOYPT
        1
    BOYPT  
       Mar 1, 2018   ❤️ 1
    POST 是 utf-8 这是固定的呀,这是浏览器,或者说 HTTP 协议这样定的。
    newbieo0O
        2
    newbieo0O  
       Mar 1, 2018   ❤️ 1
    setRequestHeader
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2798 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 01:48 · PVG 09:48 · LAX 18:48 · JFK 21:48
    ♥ Do have faith in what you're doing.