我应该如何在jQuery Ajax请求中传递查询字符串值?我目前这样做,但我相信有一种更干净的方式,不需要我手动编码。
$.ajax({
url: "ajax.aspx?ajaxid=4&UserID=" + UserID + "&EmailAddress=" + encodeURIComponent(EmailAddress),
success: function(response) {
//Do Something
},
error: function(xhr) {
//Do Something to handle error
}
});
我见过将查询字符串参数作为数组传递的示例,但这些示例不使用$.ajax()
模型,而是直接使用$.get()
。例如:
$.get("ajax.aspx", { UserID: UserID , EmailAddress: EmailAddress } );
我更喜欢使用$.ajax()格式,因为它是我所习惯的(没有特别好的理由--只是个人喜好)。
编辑2013年9月4日:
在我的问题结束后(因为“太本地化”),我发现了一个相关的(相同的)问题--有3个以上的赞成票(我的缺点是一开始就没有找到它):
Using jquery to make a POST, how to properly supply 'data' parameter?
这完美地回答了我的问题,我发现这样做更容易阅读&我不需要在URL或数据值中手动使用encodeURIComponent()
(这是我在bipen的答案中发现不清楚的)。这是因为通过$.param()
自动编码data
值)。以防这对其他人有用,下面是我使用的示例:
$.ajax({
url: "ajax.aspx?ajaxid=4",
data: {
"VarA": VarA,
"VarB": VarB,
"VarC": VarC
},
cache: false,
type: "POST",
success: function(response) {
},
error: function(xhr) {
}
});
https://stackoverflow.com/questions/15576548
复制相似问题