问题描述
我应该如何在jQuery Ajax请求中传递查询字符串值?我现在按照以下方式执行操作,但我确信有一种更简洁的方式,不需要我手动进行编码。
$ .ajax({
url:ajax.aspx?ajaxid = 4& UserID =+ UserID +& EmailAddress =+ encodeURIComponent(EmailAddress),
success:function(response){
//做某事
},
错误:function(xhr){
//做某事来处理错误
}
});
我看过一些例子,其中查询字符串参数是作为数组传递的,但是我见过的这些例子不要使用 $。ajax()
模型,而是直接使用 $。get()
。例如:
$。get(ajax.aspx,{UserID:UserID,EmailAddress:EmailAddress});
我更喜欢使用$ .ajax()格式,因为它是我习惯的特别好的理由 - 只是个人喜好)。
编辑09/04/2013:
在我的问题被关闭后(作为太本地化),我发现了一个相关的(相同的)问题 - 3个upvotes没有更少(我的不好找不到它):
这完全回答了我的问题,我发现这样做是阅读和阅读更容易我不需要在URL或DATA值中手动使用 encodeURIComponent()
(这是我在bipen的回答中发现的不清楚)。这是因为数据
值是通过)。以防万一这可以对任何人都有用,这就是我所用的例子:
$。ajax({
url:ajax.aspx?ajaxid = 4,
数据:{
VarA:VarA,
VarB:VarB,
VarC: VarC
},
cache:false,
类型:POST,
成功:函数(响应){
},
错误:函数(xhr){
}
});
使用ajax的数据选项。您可以通过ajax中的 data
选项和类型
来将数据对象发送到服务器,该类型定义您如何发送它 POST
或 GET
)。默认类型是 GET
方法
试试这个
<$ (
url:ajax.aspx,
类型:get,//通过get方法发送它
data: {
ajaxid:4,
UserID:UserID,
EmailAddress:EmailAddress
},
成功:函数(响应){
//做某事
},
错误:function(xhr){
//做某事来处理错误
}
});
您可以通过
$ _GET ['ajaxid'] //给出4
$ _GET ['UserID'] //给你发送的userid
How should I be passing query string values in a jQuery Ajax request? 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()
. 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).
Edit 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):
Using jquery to make a POST, how to properly supply 'data' parameter?
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). This is because the data
value is encoded automatically via $.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) {
}
});
Use data option of ajax. You can send data object to server by data
option in ajax and the type
which defines how you are sending it (either POST
or GET
). The default type is GET
method
Try this
$.ajax({
url: "ajax.aspx",
type: "get", //send it through get method
data: {
ajaxid: 4,
UserID: UserID,
EmailAddress: EmailAddress
},
success: function(response) {
//Do Something
},
error: function(xhr) {
//Do Something to handle error
}
});
And you can get the data by
$_GET['ajaxid'] //gives 4
$_GET['UserID'] //gives you the sent userid
这篇关于如何使用jQuery在GET请求中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!