最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - how to handle single quote in $.ajax POST (JQuery)? - Stack Overflow

programmeradmin1浏览0评论
.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?

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

Share Improve this question asked Mar 26, 2010 at 17:44 JangoJango 5,55514 gold badges59 silver badges63 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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"

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

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

You can use the pair format like this:

$.ajax({
  type: 'POST',
  url: '..serverices/ajaxserver.asmx',
  data: { "lname" : $('#lastname').val() }
});
发布评论

评论列表(0)

  1. 暂无评论