How to handle single quote in $ .ajax POST (jQuery)?

.ajax({ type: 'POST', url: '..serverices/ajaxserver.asmx', data: 'lname='+ $('#lastname').val() }); return false; 

if #lastname has one quote, it throws an error. How to deal with this?

+4
source share
3 answers

Chetan is the right to jQuery for you. But it's worth mentioning the JavaScript escape() function, which is pretty simple:

 >>> "O'Malley" "O'Malley" >>> escape("O'Malley") "O%27Malley" 
+6
source

Do not create a query string yourself when jQuery can do it for you

 data: {"lname" : $('#lastname').val()} 
+3
source

You can use the pair format as follows:

 $.ajax({ type: 'POST', url: '..serverices/ajaxserver.asmx', data: { "lname" : $('#lastname').val() } }); 
-1
source

Source: https://habr.com/ru/post/1305239/


All Articles