【问题标题】:Parameter with '&' breaking $.ajax request带有 '&' 的参数会破坏 $.ajax 请求
【发布时间】:2012-01-01 08:04:02
【问题描述】:

我正在开发一个 Web 应用程序,它在客户端使用 JavaScript + JQuery,在服务器端使用 PHP。

我想作为 AJAX 请求的参数传递的字符串之一在其内容中有一个“&”。

因此,请求的字符串被破坏。浏览器“认为”这个参数结束了,因为字符串上有一个'&'。

var hasChar = "This is a string that has a & in the content.";
var doesntHave = "This one does not contain.";
var dataString = "first=" + hasChar + "&second=" + doesntHave;

$.ajax({
    type : "POST",
    url : "myurl.php",
    data : dataString,
    cache : false,
    success : function(html) {
    }
});

服务器接收到的第一个参数为“这是一个带有”的字符串

我的问题:

如何在客户端对字符串进行编码以及如何在 PHP 服务器上对其进行解码。

【问题讨论】:

    标签: javascript jquery parameter-passing


    【解决方案1】:

    让 jQuery 为您处理hasChar(和您的其他参数)的编码:

    var hasChar = "This is a string that has a & in the content.";
    var doesntHave = "This one does not contain.";
    
    $.ajax({
        type : "POST",
        url : "myurl.php",
        data : { first: hasChar, second: doesntHave },
        cache : false,
        success : function(html) {
        }
    });
    

    【讨论】:

      【解决方案2】:

      为什么不做以下事情:

          $.ajax({
              type : "POST",
              url : "myurl.php",
              data : {
                'first': hasChar,
                'second': doesntHave
              },
              cache : false,
              success : function(html) {
              }
          });
      

      在这种情况下,jQuery 将确保字符串被正确编码。

      作为替代方案,您可以使用 encodeURIComponent() JS 的内置函数来正确编码字符串:

      var dataString = "first=" + encodeURIComponent(hasChar) + "&second=" + encodeURIComponent(doesntHave);
      

      【讨论】:

        【解决方案3】:

        您可以使用.param

        dataString = $.param({first: asChar, second: doesntHave});
        

        【讨论】:

          【解决方案4】:

          或者如果您想跳过@alex-k 提到的$.param 部分

          data : {'first': hasChar, 'second': doesntHave},
          

          【讨论】:

            【解决方案5】:

            您可以将其设置为对象:

            var hasChar = "This is a string that has a & in the content.";
            var doesntHave = "This one does not contain.";
            
            $.ajax({
                type : "POST",
                url : "myurl.php",
                data : {first: hasChar, second: doesntHave},
                cache : false,
                success : function(html) {
                }
            });
            

            【讨论】:

              【解决方案6】:

              你也可以使用encodeURI

              var encodedData = encodeURI(dataString);
              $.ajax({
                  type : "POST",
                  url : "myurl.php",
                  data : encodedData,
                  cache : false,
                  success : function(html) {
                  }
              });
              

              Link

              【讨论】:

                【解决方案7】:

                如果您想要服务器端解码/编码,请使用 urlencode()urldecode()

                【讨论】:

                • 这不是问题的真正答案。
                • @bazmegakapa:是的 - 问题已被编辑。 “如何在 server 端对字符串进行编码......”是最初的问题。
                【解决方案8】:

                试试这个

                var dataString = {};
                dataString["hasChar"] = "This is a string that has a & in the content.";
                dataString["doesntHave"] = "This one does not contain.";
                
                $.ajax({
                    type : "POST",
                    url : "myurl.php",
                    data : dataString,
                    cache : false,
                    success : function(html) {
                    }
                });
                

                【讨论】:

                  【解决方案9】:

                  您的参数需要进行简单的 URL 编码。在 PHP 方面,您不需要解码它们,一切都会正常工作。

                  有一个名为 encodeURIComponent() 的 Javascript 函数可以正确地对您的字符串进行 URL 编码。所以在基本层面上,你可以这样做:

                  var dataString = "first=" + encodeURIComponent(hasChar) + "&second=" + encodeURIComponent(doesntHave);
                  

                  如果你使用 jQuery,它会自动为你处理这个问题,如果 in your $.ajax() call you pass it an object instead of a query string:

                  data 选项可以包含以下形式的查询字符串 key1=value1&key2=value2,或 {key1: 'value1', key2: 形式的映射 '值2'}。如果使用后一种形式,则数据将转换为 在发送之前使用 jQuery.param() 查询字符串。

                  所以你只需要在你的代码中这样做:

                  var dataString = { first: hasChar, second: doesntHave);
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2013-11-11
                    • 2010-12-22
                    • 2019-05-29
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多