首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >decodeURIComponent(uri)不工作?

decodeURIComponent(uri)不工作?
EN

Stack Overflow用户
提问于 2011-05-19 11:38:58
回答 2查看 4.5K关注 0票数 0

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

代码语言:javascript
复制
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));
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-19 11:41:04

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

代码语言:javascript
复制
 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);
票数 3
EN

Stack Overflow用户

发布于 2011-05-19 11:49:12

如果您正在提交来自JS的编码值,并且想要在PHP中decode它们,您可以这样做:

代码语言:javascript
复制
// 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6053473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档