2

PHPを使用してURL文字列をurlencodingし、それをcurl経由でphantomjsスクリプトに渡し、そこでjavascriptを使用してデコードしようとしています。

私はから始めています:

localhost:7788/hi there/how are you/

これは次のようになります。

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

urlencode() 関数による php 側。

phantomjs 側には、次のものがあります。

// 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]));

コンソールの出力は次のとおりです。

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

「+」記号がスペースに置き換えられないのはなぜですか? 私はそれらを取り除こうとしていますが、関数「urldecode」がこれを行うべきであるように見えます。

4

1 に答える 1

4

rawurlencode()PHP 側では代わりに を使用する必要がurlencode()あります。スペースは符号では%20なくでエンコードされる+ため、javascript は でそれらをうまくデコードできますdecodeURIComponent()

于 2013-10-21T15:38:18.263 に答える