views:

46

answers:

3
.ajax({

type: 'POST',

url: '..serverices/ajaxserver.asmx',

data: 'lname='+ $('#lastname').val()

}); return false;

if #lastname has a single quote, it throws an error. How to handle it?

+3  A: 

Don't built the query string yourself when jQuery can do it for you

data: {"lname" : $('#lastname').val()}
Chetan Sastry
A: 

You can use the pair format like this:

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

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

>>> "O'Malley"
"O'Malley"
>>> escape("O'Malley")
"O%27Malley"
cpharmston
Although `escape` works, it is NOT what you want to use here. `encodeURIComponent` is better suited for this purpose. See http://xkr.us/articles/javascript/encode-compare/
Chetan Sastry