【问题标题】:AJAX POST and Plus ( + ) and (&) Sign How to Encode?AJAX POST 和加号 (+) 和 (&) 符号如何编码?
【发布时间】:2011-08-27 23:14:15
【问题描述】:

我正在通过AJAX to a PHP script 发布表单字段的内容并使用此代码

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

问题是任何+ and & 标志都被剥离和替换。如何安全地“编码”+ and & 标志,然后在 PHP 端适当地“解码”它?

【问题讨论】:

    标签: php ajax post decode encode


    【解决方案1】:

    需要编码你的参数encodeURIComponent 注意:-当您访问$_GET, $_POST or $_REQUEST in PHP 时,您正在检索已经解码的值。

    【讨论】:

      【解决方案2】:

      尝试:

      js:encodeURIComponent (/* url */);

      php:urldecode (/* url */);

      【讨论】:

      • 只需将代码的最后一行更改为:http.send(encodeURIComponent(params)); // 如果你愿意,然后用 urldecode 在你的服务器上捕获它...
      • 在你的 php 脚本中尝试这个处理 post 请求:foreach($_POST as $key => $value) $_POST[$key] = urldecode($value);您将在 $_POST 中将其全部解码
      • 在此处查看:w3schools.com/jsref/jsref_encodeuricomponent.asp,并查看“亲自尝试!”,以及:php.net/manual/en/function.urldecode.php
      • @metaforce :见兄弟我只想解码到我的密码 $password = $_POST["password"];我能在这里做什么
      • @metaforce: 不工作 http.send(encodeURIComponent(params));
      猜你喜欢
      • 2010-11-25
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      相关资源
      最近更新 更多