5

我写了一个 jquery ajax 代码来发表评论..

function PostComment()
{

   $.ajax({
         type :"POST",
         url:PageUrl+'Post_LectureComment',
         data:"{Comment:'"+$('#txt_PostComment').val()+"',LectureID:'"+87+"',CategoryID:'"+2+"',Author_Id:'"+ 78+"' }",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success:SuccessHandler ,  
         });

         function SuccessHandler(data)
         {}
}

当我使用 'like =durgesh'rao 在 txt_PostComment 中发送数据时,它显示错误

请求负载:{评论:'durgesh'rao',LectureID:'1250',CategoryID:'2',Author_Id:'135'}

有什么方法可以用' ???

4

3 回答 3

5

我相信您正在尝试构建包含该'字符的 JSON 对象。所以要解决这个问题,你首先需要处理字符串'

function replacequote(text) {
    var newText = "";
    for (var i = 0; i < text.length; i++) {
        if (text[i] == "'") {
            newText += "\\'";
        }
        else
            newText += text[i];
    }
    return newText;
};


function PostComment()
{
   $.ajax({
         type :"POST",
         url:PageUrl+'Post_LectureComment',
         data:"{Comment:'" + replacequote($('#txt_PostComment').val()) + "',LectureID:'"+87+"',CategoryID:'"+2+"',Author_Id:'"+ 78+"' }",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success:SuccessHandler ,  
         });

         function SuccessHandler(data)
         {}
}
于 2013-02-25T10:42:07.600 回答
1

无需使用字符串文字来构建对象。只需创建一个新对象并设置适当的属性。

$.ajax({
         type :"POST",
         url:PageUrl+'Post_LectureComment',
         data:{comment: $('#txt_PostComment').val(),lectureID:"87",categoryID:"2",author_Id:"78"},
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success:SuccessHandler  
         });
于 2013-02-25T10:31:04.227 回答
0

您可以编辑您的查询。
至于你'是问题的创造者,在搜索时你可以这样查询:

SET ESCAPE "'"
SELECT * FROM x_table WHERE txt_PostComment with ' like "%durgesh'rao%";
于 2013-02-25T10:50:55.623 回答