V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
sunhk25
V2EX  ›  问与答

ajax 传输数据的编码问题

  •  
  •   sunhk25 · 2018-03-01 09:29:07 +08:00 · 1759 次点击
    这是一个创建于 2262 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 页面不是 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 条回复    2018-03-01 10:52:16 +08:00
    BOYPT
        1
    BOYPT  
       2018-03-01 09:32:33 +08:00   ❤️ 1
    POST 是 utf-8 这是固定的呀,这是浏览器,或者说 HTTP 协议这样定的。
    newbieo0O
        2
    newbieo0O  
       2018-03-01 10:52:16 +08:00   ❤️ 1
    setRequestHeader
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1543 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 17:12 · PVG 01:12 · LAX 10:12 · JFK 13:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.