Jquery ajax sending data with special character

I have a wriiten 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 send data to txt_PostComment with 'like = durgesh'rao it shows an error

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

- any way to send data using <???

+4
source share
3 answers

I believe that you are trying to create a JSON object containing the character ' . Therefore, to solve this problem, you need to process the strings first 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) {} } 
+5
source

There is no need to build an object using a string literal. Just 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 }); 
+1
source

You can edit the request. when searching, you can make a request as follows. as for you "is the creator of the problem.

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

0
source

All Articles