Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax sending data with special character [duplicate]

Tags:

jquery

i have wriiten a jquery ajax code of posing comment..

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)
         {}
}

when i am sending data in txt_PostComment with ' like =durgesh'rao it is showing error

Request Payload: {Comment: 'durgesh'rao',LectureID:'1250',CategoryID:'2',Author_Id:'135' }

is any way to send data with ' ???

like image 923
DurGesh kuMar RAO Avatar asked Feb 25 '13 10:02

DurGesh kuMar RAO


1 Answers

I believe you r trying to build JSON object that contain the ' character. So to solve the this problem you need first to handle the strings with '

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)
         {}
}
like image 122
ebram khalil Avatar answered Sep 28 '22 07:09

ebram khalil