debugURIComponent(uri) 不起作用?

发布于 2024-11-08 04:50:39 字数 817 浏览 0 评论 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));

I'm using Ajax to post contents, and I'm using http.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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饭团 2024-11-15 04:50:39

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

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

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);
小苏打饼 2024-11-15 04:50:39

如果您从 JS 提交编码值并希望在 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文