5

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 ' ???

3 Answers3

5

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)
         {}
}
ebram khalil
  • 8,252
  • 7
  • 42
  • 60
1

There is no need to build an object using a String literal. Simply create a new object and set the appropriate properties.

$.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  
         });
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • ur method not working in my case.. showing error ..Request URL:http://localhost:6459/SkyLearning/DL/WebService_SKY.aspx/Post_LectureComment Request Method:POST Status Code:500 Internal Server Error; request payload::Comment=furgesdh&LectureID=1250&CategoryID=2&Author_Id=135 – DurGesh kuMar RAO Feb 25 '13 at 10:38
  • a 500 server error usually means something went wrong on the server's side. have you looked at your server's error log? (like PHP Error Log) to see why you are getting a 500 error? – David 'the bald ginger' Feb 25 '13 at 10:41
  • @^ i have send data as data:{Comment:$('#txt_PostComment').val(),LectureID:$('.lbl_LectureID').text(),CategoryID:"2",Author_Id:$('#content_Type').attr('data-lectureauthorid')}, but not working.. – DurGesh kuMar RAO Feb 25 '13 at 10:43
  • in browser preview i have seen..{"Message":"InvalidJSONprimitive:Comment.","StackTrace":"atSystem.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\natSystem.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32depth)\r\natSystem.Web.Script.Serializat.... – DurGesh kuMar RAO Feb 25 '13 at 10:44
  • I updated give it a shot. (removed extra comma, lowercased property names) – Kevin Bowersox Feb 25 '13 at 10:47
0

You can edit you query.
As for you ' is the problem creator, while searching you can make the query like this:

SET ESCAPE "'"
SELECT * FROM x_table WHERE txt_PostComment with ' like "%durgesh'rao%";
GileBrt
  • 1,830
  • 3
  • 20
  • 28
Tarun Kumar
  • 508
  • 3
  • 14