【问题标题】:How can I use json data with & symbol using json如何使用带有 & 符号的 json 数据使用 json
【发布时间】:2021-06-09 19:09:05
【问题描述】:

我已尝试使用以下代码:

$.get("/a.php?op=ajax&section=b&cat_name=" + name.replaceAll("%", "")) + "&a=" + aname.replaceAll("%", "_"),function(resp) {

});

在我的name 值中包含多个& 符号。那时我无法传递完整的值,因为当出现一个 & 符号时,它会破坏我的数据。 例如我的name = 'DisplayPort & Mini DisplayPort Connector' 我只得到了“DisplayPort”。我没有得到剩余的部分。

【问题讨论】:

    标签: javascript php json ajax


    【解决方案1】:

    使用encodeURIComponent() 对查询参数中的特殊字符进行编码。如果你这样做,你也不需要删除%,它会正确编码它们。

    $.get("/a.php?op=ajax&section=b&cat_name=" + encodeURIComponent(name) + "&a=" + encodeURIComponent(aname),function(resp) {
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-30
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2021-08-17
      相关资源
      最近更新 更多