jQuery 如何使用jQuery在GET请求中传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15576548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameters in GET requests with jQuery
提问by HeavenCore
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.
我应该如何在 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
}
});
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:
我见过查询字符串参数作为数组传递的示例,但我见过的这些示例没有使用$.ajax()
模型,而是直接转到$.get()
. 例如:
$.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:
2013 年 9 月 4 日编辑:
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):
在我的问题结束后(因为“太本地化”),我发现了一个相关(相同)的问题 - 不少于 3 个赞成票(我很遗憾没有首先找到它):
Using jquery to make a POST, how to properly supply 'data' parameter?
使用 jquery 进行 POST,如何正确提供“数据”参数?
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:
这完美地回答了我的问题,我发现这样做更容易阅读并且我不需要encodeURIComponent()
在 URL 或 DATA 值中手动使用(这是我在 bipen 的回答中发现不清楚的)。这是因为该data
值是通过$.param()
)自动编码的。以防万一这对其他人有用,这是我使用的示例:
$.ajax({
url: "ajax.aspx?ajaxid=4",
data: {
"VarA": VarA,
"VarB": VarB,
"VarC": VarC
},
cache: false,
type: "POST",
success: function(response) {
},
error: function(xhr) {
}
});
回答by bipen
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
使用ajax的数据选项。您可以通过data
ajax 中的选项将数据对象发送到服务器,type
它定义了您发送它的方式(POST
或GET
)。默认类型是GET
方法
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 (if you are using PHP)
您可以通过以下方式获取数据(如果您使用的是 PHP)
$_GET['ajaxid'] //gives 4
$_GET['UserID'] //gives you the sent userid
In aspx, I believe it is (might be wrong)
在aspx中,我相信它是(可能是错误的)
Request.QueryString["ajaxid"].ToString();
回答by Cianan Sims
回答by theterminalguy
Here is the syntax using jQuery $.get
这是使用 jQuery 的语法 $.get
$.get(url, data, successCallback, datatype)
So in your case, that would equate to,
所以在你的情况下,这相当于,
var url = 'ajax.asp';
var data = { ajaxid: 4, UserID: UserID, EmailAddress: EmailAddress };
var datatype = 'jsonp';
function success(response) {
// do something here
}
$.get('ajax.aspx', data, success, datatype)
Note$.get
does not give you the opportunity to set an error handler. But there are several ways to do it either using $.ajaxSetup(), $.ajaxError()or chaining a .fail
on your $.get
like below
注意$.get
没有给你设置错误处理程序的机会。但是有几种方法可以使用$.ajaxSetup(),$.ajaxError()或.fail
在$.get
下面链接 a
$.get(url, data, success, datatype)
.fail(function(){
})
The reason for setting the datatype as 'jsonp' is due to browser same origin policy issues, but if you are making the request on the same domain where your javascript is hosted, you should be fine with datatype set to json
.
将数据类型设置为 'jsonp' 的原因是由于浏览器同源策略问题,但如果您在托管 javascript 的同一域上发出请求,则将数据类型设置为json
.
If you don't want to use the jquery $.get
then see the docsfor $.ajax
which allows room for more flexibility
如果你不想使用jQuery的$.get
,然后看到文档的$.ajax
,它允许更多空间的灵活性
回答by Jai
Try adding this:
尝试添加这个:
$.ajax({
url: "ajax.aspx",
type:'get',
data: {ajaxid:4, UserID: UserID , EmailAddress: encodeURIComponent(EmailAddress)},
dataType: 'json',
success: function(response) {
//Do Something
},
error: function(xhr) {
//Do Something to handle error
}
});
Depends on what datatype is expected, you can assign html, json, script, xml
取决于预期的数据类型,您可以分配 html, json, script, xml
回答by Webinan
Had the same problem where I specified data
but the browser was sending requests to URL ending with [Object object]
.
在我指定的地方遇到了同样的问题,data
但浏览器正在向以[Object object]
.
You should have processData
set to true
.
你应该processData
设置为true
.
processData: true, // You should comment this out if is false or set to true
回答by Danwilliger
The data property allows you to send in a string. On your server side code, accept it as a string argument name "myVar" and then you can parse it out.
data 属性允许您发送一个字符串。在您的服务器端代码中,接受它作为字符串参数名称“myVar”,然后您可以解析它。
$.ajax({
url: "ajax.aspx",
data: [myVar = {id: 4, email: 'emailaddress', myArray: [1, 2, 3]}];
success: function(response) {
//Do Something
},
error: function(xhr) {
//Do Something to handle error
}
});
回答by Sterling Archer
You can use the $.ajax()
, and if you don't want to put the parameters directly into the URL, use the data:
. That's appended to the URL
您可以使用$.ajax()
,如果您不想将参数直接放入 URL 中,请使用data:
. 这是附加到 URL
回答by ujjal
The data parameter of ajax method allows you send data to server side.On server side you can request the data.See the code
ajax方法的data参数允许你向服务器端发送数据。在服务器端你可以请求数据。见代码
var id=5;
$.ajax({
type: "get",
url: "url of server side script",
data:{id:id},
success: function(res){
console.log(res);
},
error:function(error)
{
console.log(error);
}
});
At server side receive it using $_GET variable.
在服务器端使用 $_GET 变量接收它。
$_GET['id'];