2012-03-03 95 views
0

我已經遍佈網絡尋找解決這個問題的方法。但找不到解決辦法。發送時jQuery 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,但是它將雙引號添加到問題標記中,並且它完全刪除了字符串中的&,所以它似乎不是理想的解決方案。

預先感謝您。

+0

絕對。對於那個很抱歉。我是新來的。 – 2012-03-03 04:01:24

回答

2

您需要URL編碼消息:

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

非常感謝。這工作完美! – 2012-03-03 04:08:31

4

如果使用數據對象不應該有任何問題。讓jQuery的編碼它

data: {type: 'type', msg: 'msg', msgid: 'msgid'} 
相關問題