来来来看看, url 中的 + 号问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
whatisnew

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

  •  
  •   whatisnew 2015 年 5 月 8 日 2583 次点击
    这是一个创建于 4003 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个日期 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 年 5 月 8 日
    urlencode
    liuhaotian
        2
    liuhaotian  
       2015 年 5 月 8 日
    +是空格被encode之后的结果,这时候你需要进行 urlencode 获取后进行 urldecode
    jasonding
        3
    jasonding  
       2015 年 5 月 8 日
    加号和空格的互转问题,你可以encode/decode三次来破解这个黑魔法。不过如果没有特殊原因的话,replace就够了
    whatisnew
        4
    whatisnew  
    OP
       2015 年 5 月 8 日
    @liuhaotian 那上 + 号是字符串,不是 encode 之后的
    lianyue
        5
    lianyue  
       2015 年 5 月 8 日
    %20 变成
    Septembers
        6
    Septembers  
       2015 年 5 月 8 日
    使用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 年 5 月 8 日
    HttpServer decode会把 + 转为空格,把"+" replace 为 “%2B”
    MaiCong
        8
    MaiCong  
       2015 年 5 月 8 日
    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 年 5 月 8 日
    @whatisnew 我的意思是,这个+是空格encode的结果。因此已经被识别为空格了。
    Septembers
        10
    Septembers  
       2015 年 5 月 8 日
    @liuhaotian 我引用的规范里有说
    msg7086
        11
    msg7086  
       2015 年 5 月 8 日
    过url传递前必须先encode。
    CodeDrift
        12
    CodeDrift  
       2015 年 5 月 8 日
    neilwong
        13
    neilwong  
       2015 年 5 月 8 日
    我奇怪的是get传递参数为什么不是timestamp而是这么一串字符串~
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5193 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 71ms UTC 03:43 PVG 11:43 LAX 20:43 JFK 23:43
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86