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

来来来看看, url 中的 + 号问题

  •  
  •   whatisnew · 2015-05-08 10:52:46 +08:00 · 2108 次点击
    这是一个创建于 3284 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个日期 Fri, 11 May 2014 01:23:45 +0000 经 url 传递(get),然后,
    就变成了 Fri, 11 May 2014 01:23:45 0000 注意 + 号没有了

    这个好像是 w3c 的标准。。。大家是怎么解决的?

    13 条回复    2015-05-08 16:31:31 +08:00
    free9fw
        1
    free9fw  
       2015-05-08 11:04:22 +08:00
    urlencode
    liuhaotian
        2
    liuhaotian  
       2015-05-08 11:08:14 +08:00
    +是空格被encode之后的结果,这时候你需要进行 urlencode 获取后进行 urldecode
    jasonding
        3
    jasonding  
       2015-05-08 11:20:03 +08:00
    加号和空格的互转问题,你可以encode/decode三次来破解这个黑魔法。不过如果没有特殊原因的话,replace就够了
    whatisnew
        4
    whatisnew  
    OP
       2015-05-08 11:21:53 +08:00
    @liuhaotian 那上 + 号是字符串,不是 encode 之后的
    lianyue
        5
    lianyue  
       2015-05-08 11:23:24 +08:00
    %20 变成
    Septembers
        6
    Septembers  
       2015-05-08 11:27:16 +08:00
    使用encodeURIComponent
    > encoded = encodeURIComponent('Fri, 11 May 2014 01:23:45 +0000')
    < "Fri%2C%2011%20May%202014%2001%3A23%3A45%20%2B0000"
    > decodeURIComponent(encoded)
    < "Fri, 11 May 2014 01:23:45 +0000"
    see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
    see http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
    see http://en.wikipedia.org/wiki/Percent-encoding#The_application.2Fx-www-form-urlencoded_type

    Base64
    > encoded = btoa('Fri, 11 May 2014 01:23:45 +0000')
    < "RnJpLCAxMSBNYXkgMjAxNCAwMToyMzo0NSArMDAwMA=="
    > atob(encoded)
    < "Fri, 11 May 2014 01:23:45 +0000"

    对于这种格式(GMT)的时间建议用Date预处理成ISO 8601
    > new Date('Fri, 11 May 2014 01:23:45 +0000').toISOString()
    < "2014-05-11T01:23:45.000Z"
    ISO 8601格式相对问题少些
    cpp255
        7
    cpp255  
       2015-05-08 11:31:42 +08:00
    HttpServer decode会把 + 转为空格,把"+" replace 为 “%2B”
    MaiCong
        8
    MaiCong  
       2015-05-08 11:32:43 +08:00
    http://www.w3school.com.cn/jsref/jsref_encodeURIComponent.asp
    http://www.w3school.com.cn/jsref/jsref_decodeURIComponent.asp

    var time = encodeURIComponent('Fri, 11 May 2014 01:23:45 +0000');

    console.log(time + '\n' + decodeURIComponent(a));
    liuhaotian
        9
    liuhaotian  
       2015-05-08 11:36:27 +08:00
    @whatisnew 我的意思是,这个+是空格encode的结果。因此已经被识别为空格了。
    Septembers
        10
    Septembers  
       2015-05-08 11:39:11 +08:00
    @liuhaotian 我引用的规范里有说
    msg7086
        11
    msg7086  
       2015-05-08 11:54:45 +08:00
    过url传递前必须先encode。
    Anybfans
        12
    Anybfans  
       2015-05-08 13:22:07 +08:00
    neilwong
        13
    neilwong  
       2015-05-08 16:31:31 +08:00
    我奇怪的是get传递参数为什么不是timestamp而是这么一串字符串~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2352 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:20 · PVG 14:20 · LAX 23:20 · JFK 02:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.