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().

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改