decodeURIComponent(uri) 不起作用


decodeURIComponent(uri) is not working?

我正在使用Ajax发布内容,并且我正在使用http.send(encodeURIComponent(params));进行编码...但我无法用 PHP 解码它们。我正在使用 POST,所以我认为它不需要编码?我对我应该如何解码 PHP 中的值感到困惑......

params = "guid="+szguid+"&username="+szusername+"&password="+szpassword+"&ip="+ip+"&name="+name+"&os="+os;
        //alert(params);
        document.body.style.cursor = 'wait';//change cursor to wait
        if(!http)
            http = CreateObject();  
        nocache = Math.random();
        http.open('post', 'addvm.php');
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");
        http.onreadystatechange = SaveReply;
       http.send(encodeURIComponent(params));

encodeURIComponent将对所有分隔键/值对的 &s 和 =s 进行编码。您需要在每个零件上单独使用它。这样:

 params = 
 "guid="      + encodeURIComponent(szguid)     + 
 "&username=" + encodeURIComponent(szusername) +
 "&password=" + encodeURIComponent(szpassword) +
 "&ip="       + encodeURIComponent(ip)         +
 "&name="     + encodeURIComponent(name)       +
 "&os="       + encodeURIComponent(os);
    //alert(params);
    document.body.style.cursor = 'wait';//change cursor to wait
    if(!http)
        http = CreateObject();  
    nocache = Math.random();
    http.open('post', 'addvm.php');
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
    http.onreadystatechange = SaveReply;
    http.send(params);

如果你从JS提交编码值,并希望在PHP中decode它们,你可以做如下的事情:

// decode POST values so you don't have to decode each pieces one by one
$_POST = array_map(function($param) {
            return urldecode($param);
        }, $_POST);
// assign post values after every value is decoded