开发者

decodeURIComponent(uri) is not working?

开发者 https://www.devze.com 2023-03-06 18:20 出处:网络
I\'m using Ajax to post contents, and I\'m using htt开发者_如何学Cp.send(encodeURIComponent(params)); for encoding ... but I\'m unable to decode them in PHP. I\'m using POST so I didn\'t think it requ

I'm using Ajax to post contents, and I'm using htt开发者_如何学Cp.send(encodeURIComponent(params)); for encoding ... but I'm unable to decode them in PHP. I'm using POST so I didn't think it required encode? I'm confused as to how I'm supposed to decode the values in 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 will encode all the &s and =s that delimit your key/value pairs. You need to use it on each part individually. Like so:

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


If you're submitting encoded values from JS and want to decode them in PHP you can do something like:

// 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
0

精彩评论

暂无评论...
验证码 换一张
取 消