2014-12-02 55 views
1

我有這樣的ajax:Ajax調用失敗時的參數包含HTML標籤

$.ajax({ 
     url: '/PostComment/AddComment', 
     type: 'POST', 
     dataType: 'json', 
     cache: false, 
     data: { "PostId": PostId, "CommentText": CommentText }, 
     success: function (data){ 
      alert('Ok'); 
     } 
}); 

的問題是,當CommentText變量包含任何HTML標籤Ajax調用失敗。我知道這是一個奇怪的問題,但這是發生的。

+1

向我們展示'/ PostComment/AddComment'中的內容,以及CommentText中的內容。也許你想要執行數據庫操作,並且你的數據不會被轉義/清理。 – vaso123 2014-12-02 11:36:16

+0

你得到的錯誤代碼/消息是什麼 – 2014-12-02 11:36:49

+0

錯誤在於ajax沒有調用任何函數 – 2014-12-02 11:52:45

回答

2

嘗試發送編碼值到服務器端:

commentText = encodeURIComponent(commentText); 

在服務器端,如果您使用的是Java,那麼你可以做:

String commentStr = URLDecoder.decode(request.getParameter("commentText"), "UTF-8"); 
+0

這個解決方案是否工作? – ArinCool 2014-12-02 11:48:31

+0

索裏,不工作。 – 2014-12-02 11:51:34

+0

你能發佈你的完整代碼嗎? – ArinCool 2014-12-02 12:01:13