jquery ajax发送具有特殊字符的数据

时间:2013-02-25 10:27:51

标签: jquery

我有一个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)
         {}
}

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

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

是使用'???

发送数据的任何方式

3 个答案:

答案 0 :(得分: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)
         {}
}

答案 1 :(得分:1)

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

$.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  
         });

答案 2 :(得分:0)

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

SET ESCAPE“'” SELECT * FROM x_table WHERE txt_PostComment with'like“%durgesh'rao%”;

相关问题