0

我正在使用 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));
4

2 回答 2

3

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);
于 2011-05-19T03:41:03.560 回答
1

如果您从 JS 提交编码值并希望decode在 PHP 中使用它们,您可以执行以下操作:

// 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
于 2011-05-19T03:49:11.933 回答