[英]How to pass query string parameter to ajax call from data
I'm using ajax call for hit the API & i want to send an query parameter, How can i send query parameter with ajax call. 我正在使用ajax调用来命中API,我想发送查询参数,我该如何使用ajax调用发送查询参数。 Here is my code.
这是我的代码。 Any help on this will be appreciated, Thanks.
谢谢您的帮助。
$("[id*=btnok]").click(function (e) {
e.preventDefault();
var obj = {};
obj = $.trim($("[id*=nodays]").val());
$.ajax({
url: "/apilink",
"data": {
"api": "api",
"params": "?userType=driver&type=true&count=" + obj
},
type: "post",
dataType: "json",
success: function (r) {
console.log(r)
}
});
});
As you're sending a POST request any properties in the data
object will be added to the body of the request. 在发送POST请求时,
data
对象中的所有属性都会添加到请求的正文中。 To force a value to be sent in the querystring you need to manually append it to the string you provide to the url
property. 要强制在查询字符串中发送值,您需要将其手动添加到提供给
url
属性的字符串中。
Also note that defining obj
as an empty object is redundant as $.trim()
always returns a string. 还要注意,将
obj
定义为空对象是多余的,因为$.trim()
始终返回字符串。
$("[id*=btnok]").click(function (e) {
e.preventDefault();
var obj = $.trim($("[id*=nodays]").val());
$.ajax({
url: "/apilink?params=" + obj,
data: {
api: "api",
},
type: "post",
dataType: "json",
success: function (r) {
console.log(r)
}
});
});
As I see you make a post request so parameters are passed in params
property in request body. 如我所见,您发出了发布请求,因此参数在请求正文的
params
属性中传递。 But as an option you can add string params to your /apilink
. 但是,您可以选择将字符串参数添加到
/apilink
。 Like that: /apilink?param1=val1¶m2=val2
像这样:
/apilink?param1=val1¶m2=val2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.