dongxiongshi9952 2013-10-21 15:31
浏览 35
已采纳

麻烦在javascript解码网址

I am urlencoding a url string using php and then passing it via curl to a phantomjs script where I am trying to decode it using javascript.

I am starting with:

localhost:7788/hi there/how are you/

which gets turned into:

 localhost:7788/hi+there%2Fhow+are+you

on the php side by the urlencode() function.

On the phantomjs side , I have:

// Create serever and listen port 
server.listen(port, function(request, response) {    

function urldecode(str) {
   return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}    


        // Print some information Just for debbug 
        console.log("We got some requset !!!"); 
        console.log("request method: ", request.method);  // request.method POST or GET 
        console.log("Get params: ", request.url); //     

           url= urldecode(request.url);

        //-- Split requested params
      var requestparams = request.url.split('/');    


      console.log(urldecode(requestparams[1]));
      console.log(urldecode(requestparams[2]));

The output at the console is :

.....
request method:  GET
Get params:  /hi%2Bthere/how%2Bare%2Byou
hi+there
how+are+you

Why are the '+' signs not replaced with spaces? I'm trying to get rid of them and it looks to me that the function 'urldecode' should do this.

  • 写回答

1条回答 默认 最新

  • drsl90685154 2013-10-21 15:38
    关注

    You should use rawurlencode() instead of urlencode() in the PHP side, so spaces are encoded with %20 and not+ signs, so javascript can decode them well with decodeURIComponent().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口