j查询 AJAX POST JSON 发送时出错?或 & 到服务器



我在网上到处寻找这个问题的解决方案。但找不到解决方案。

以下是我为jQuery准备的:

    function fetchMsg (type, msg, msgid) {
    $.ajax({
        type: "POST",
        url: "fetchmsg.php",
        data: "type=" + type + "&msg=" + msg + "&msgid=" + msgid,
        dataType: "json",
        cache: false,
        success: function(data){
            $.each(data.msgs, function(i,item){
                $('.messages ul').append("<div class='msgstyle id='"+item.usid+"'><li class='msgname'>"+item.fname+" "+item.lname+"</li>"+"<li class='msgid' id='"+item.msgid+"'>"+item.msg+"</li></div>");
            });
        }
    });
    return false;
}

问题是我打字的时候?或者&作为一条消息,我得到了一个错误,例如:jQuery17104689251377712935_1330746677552

有什么建议吗?这实际上是插入数据库中的消息(而不是实际的?或&字符)。我尝试过JSON.stringify,但它在问号上添加了双引号,并完全删除了&在字符串中,所以这似乎不是一个理想的解决方案。

提前谢谢。

如果使用数据对象,应该不会有任何问题。让jQuery将其编码为

 data: {type: 'type', msg: 'msg', msgid: 'msgid'}

您需要URL编码消息:

data: "type=" + type + "&msg=" + encodeURIComponent(msg) + "&msgid=" + msgid,

相关内容

最新更新