如何在$ .ajax POST(JQuery)中处理单引号?

时间:2022-11-29 14:03:03
.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?

如果#lastname有一个引号,则会引发错误。怎么处理?

3 个解决方案

#1


6  

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

Chetan是正确的on-jQuery处理方式。但是,值得一提的是JavaScript escape()函数,这非常简单:

>>> "O'Malley"
"O'Malley"
>>> escape("O'Malley")
"O%27Malley"

#2


3  

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

当jQuery可以为您完成时,不要自己构建查询字符串

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

#3


-1  

You can use the pair format like this:

您可以使用这样的对格式:

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

#1


6  

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

Chetan是正确的on-jQuery处理方式。但是,值得一提的是JavaScript escape()函数,这非常简单:

>>> "O'Malley"
"O'Malley"
>>> escape("O'Malley")
"O%27Malley"

#2


3  

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

当jQuery可以为您完成时,不要自己构建查询字符串

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

#3


-1  

You can use the pair format like this:

您可以使用这样的对格式:

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