我有这个 ajax :
$.ajax({
url: '/PostComment/AddComment',
type: 'POST',
dataType: 'json',
cache: false,
data: { "PostId": PostId, "CommentText": CommentText },
success: function (data){
alert('Ok');
}
});
问题是,当 CommentText 变量包含任何 html 标记时,ajax 调用将失败。我知道这是一个奇怪的问题,但事实就是如此。
最佳答案
尝试将编码值发送到服务器端:
commentText = encodeURIComponent(commentText);
在服务器端,如果你使用的是Java,那么你可以这样做:
String commentStr = URLDecoder.decode(request.getParameter("commentText"), "UTF-8");
关于javascript - 当参数包含 html 标记时 Ajax 调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27248409/