How should I be passing query string values in a jQuery Ajax request?(我应该如何在jQuery Ajax请求中传递查询字符串值?)
I currently do them as follows but I'm sure there is a cleaner way that does not require me to encode manually.(我目前按照以下方式执行它们,但我确信有一种更简洁的方法,不需要我手动编码。)$.ajax({
url: "ajax.aspx?ajaxid=4&UserID=" + UserID + "&EmailAddress=" + encodeURIComponent(EmailAddress),
success: function(response) {
//Do Something
},
error: function(xhr) {
//Do Something to handle error
}
});
I've seen examples where query string parameters are passed as an array but these examples I've seen don't use the $.ajax()
model, instead they go straight to $.get()
.(我已经看到了查询字符串参数作为数组传递的示例,但是我见过的这些示例不使用$.ajax()
模型,而是直接使用$.get()
。) For example:(例如:)
$.get("ajax.aspx", { UserID: UserID , EmailAddress: EmailAddress } );
I prefer to use the $.ajax() format as it's what I'm used to (no particularly good reason - just a personal preference).(我更喜欢使用$ .ajax()格式,因为它是我习惯的(没有特别好的理由 - 仅仅是个人偏好)。)
Edit 09/04/2013:(编辑09/04/2013:)
After my question was closed (as "Too Localised") i found a related (identical) question - with 3 upvotes no-less (My bad for not finding it in the first place):(在我的问题被关闭后(作为“Too Localized”),我发现了一个相关的(完全相同的)问题 - 3个upvotes no-less(我不喜欢首先找到它):)
Using jquery to make a POST, how to properly supply 'data' parameter?(使用jquery进行POST,如何正确提供'data'参数?)
This answered my question perfectly, I found that doing it this way is much easier to read & I don't need to manually use encodeURIComponent()
in the URL or the DATA values (which is what i found unclear in bipen's answer).(这完全回答了我的问题,我发现这样做更容易阅读,我不需要在URL或DATA值中手动使用encodeURIComponent()
(这是我在bipen的答案中发现的不清楚)。) This is because the data
value is encoded automatically via $.param()
).(这是因为data
值是通过$.param()
自动编码的。) Just in case this can be of use to anyone else, this is the example I went with:(为了防止这对任何人都有用,这就是我的例子:)
$.ajax({
url: "ajax.aspx?ajaxid=4",
data: {
"VarA": VarA,
"VarB": VarB,
"VarC": VarC
},
cache: false,
type: "POST",
success: function(response) {
},
error: function(xhr) {
}
});
ask by HeavenCore translate from so