jQuery 如何在 $ajax POST 中传递参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18697034/
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 $ajax POST?
提问by user4127
I have followed the tutorial as stated in thislink. In the code below for some reason the data is not appended to the url as parameters, but if I set them directly to the url using /?field1="hello"
it works.
我已按照此链接中所述的教程进行操作。在下面的代码中,由于某种原因,数据没有作为参数附加到 url 中,但是如果我使用/?field1="hello"
它直接将它们设置为 url就可以了。
$.ajax({
url: 'superman',
type: 'POST',
data: { field1: "hello", field2 : "hello2"} ,
contentType: 'application/json; charset=utf-8',
success: function (response) {
alert(response.status);
},
error: function () {
alert("error");
}
});
采纳答案by Alvaro
I would recommend you to make use of the $.post
or $.get
syntax of jQuery for simple cases:
对于简单的情况,我建议您使用jQuery的$.post
or$.get
语法:
$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
});
If you need to catch the fail cases, just do this:
如果您需要捕获失败案例,只需执行以下操作:
$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
}).fail(function(){
console.log("error");
});
Additionally, if you always send a JSON string, you can use $.getJSONor $.post with one more parameter at the very end.
此外,如果你总是发送一个 JSON 字符串,你可以使用$.getJSON或 $.post 在最后加上一个参数。
$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
}, 'json');
回答by Ajith S
Try using GET method,
尝试使用 GET 方法,
var request = $.ajax({
url: 'url',
type: 'GET',
data: { field1: "hello", field2 : "hello2"} ,
contentType: 'application/json; charset=utf-8'
});
request.done(function(data) {
// your success code here
});
request.fail(function(jqXHR, textStatus) {
// your failure code here
});
You cannot see parameters in URL with POST method.
您无法使用 POST 方法查看 URL 中的参数。
Edit:
编辑:
Deprecation Notice:The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
弃用通知:jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调从 jQuery 3.0 开始被移除。您可以改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。
回答by Stephen Ostermiller
Jquery.ajaxdoes not encode POST data for you automatically the way that it does for GET data. Jquery expects your data to be pre-formated to append to the request body to be sent directly across the wire.
Jquery.ajax不会像对 GET 数据那样自动为您编码 POST 数据。Jquery 期望您的数据预先格式化以附加到直接通过网络发送的请求正文。
A solution is to use the jQuery.paramfunction to build a query string that most scripts that process POST requests expect.
一种解决方案是使用jQuery.param函数来构建大多数处理 POST 请求的脚本所期望的查询字符串。
$.ajax({
url: 'superman',
type: 'POST',
data: jQuery.param({ field1: "hello", field2 : "hello2"}) ,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function (response) {
alert(response.status);
},
error: function () {
alert("error");
}
});
In this case the param
method formats the data to:
在这种情况下,该param
方法将数据格式化为:
field1=hello&field2=hello2
The Jquery.ajaxdocumentation says that there is a flag called processData
that controls whether this encoding is done automatically or not. The documentation says that it defaults to true
, but that is not the behavior I observe when POST
is used.
该Jquery.ajax文档中说,有一个叫标志processData
控制该编码是否是自动还是没有这样做。文档说它默认为true
,但这不是我在POST
使用时观察到的行为。
回答by SiwachGaurav
function FillData() {
var param = $("#<%= TextBox1.ClientID %>").val();
$("#tbDetails").append("<img src='Images/loading.gif'/>");
$.ajax({
type: "POST",/*method type*/
contentType: "application/json; charset=utf-8",
url: "Default.aspx/BindDatatable",/*Target function that will be return result*/
data: '{"data":"' + param + '"}',/*parameter pass data is parameter name param is value */
dataType: "json",
success: function(data) {
alert("Success");
}
},
error: function(result) {
alert("Error");
}
});
}
回答by Denys Séguret
In a POST request, the parameters are sent in the body of the request, that's why you don't see them in the URL.
在POST 请求中,参数在请求正文中发送,这就是为什么您在 URL 中看不到它们的原因。
If you want to see them, change
如果你想看到他们,改变
type: 'POST',
to
到
type: 'GET',
Note that browsers have development tools which lets you see the complete requests that your code issues. In Chrome, it's in the "Network" panel.
请注意,浏览器具有开发工具,可让您查看代码发出的完整请求。在 Chrome 中,它位于“网络”面板中。
回答by Dulith De Costa
$.ajax(
{
type: 'post',
url: 'superman',
data: {
"field1": "hello",
"field2": "hello1"
},
success: function (response) {
alert("Success !!");
},
error: function () {
alert("Error !!");
}
}
);
type: 'POST'
, will append **parametersto the body of the request** which is not seenin the URLwhile type: 'GET'
, appends parameters to the URL which is visible.
type: 'POST'
将**参数附加到其被请求**的主体没有看到在URL而 type: 'GET'
,附加参数,其是URL可见。
Most of the popular web browsers contain network panels which displays the complete request.
大多数流行的网络浏览器都包含显示完整请求的网络面板。
In network panel select XHRto see requests.
在网络面板中选择XHR以查看请求。
This can also be done via this.
这也可以通过this来完成。
$.post('superman',
{
'field1': 'hello',
'field2': 'hello1'
},
function (response) {
alert("Success !");
}
);
回答by Shrinivas
You can do it using $.ajax or $.post
你可以使用 $.ajax 或 $.post
Using $.ajax :
使用 $.ajax :
$.ajax({
type: 'post',
url: 'superman',
data: {
'field1': 'hello',
'field2': 'hello1'
},
success: function (response) {
alert(response.status);
},
error: function () {
alert("error");
}
});
Using $.post :
使用 $.post :
$.post('superman',
{
'field1': 'hello',
'field2': 'hello1'
},
function (response, status) {
alert(response.status);
}
);
回答by Sylvester Das
Your code was right except you are not passing the JSON keys as strings.
您的代码是正确的,只是您没有将 JSON 键作为字符串传递。
It should have double or single quotes around it
它应该有双引号或单引号
{ "field1": "hello", "field2" : "hello2"}
{ "field1": "hello", "field2": "hello2"}
$.ajax(
{
type: 'post',
url: 'superman',
data: {
"field1": "hello", // Quotes were missing
"field2": "hello1" // Here also
},
success: function (response) {
alert(response);
},
error: function () {
alert("error");
}
}
);
回答by Ali.MD
For send parameters in url in POST
method You can simply append it to url like this:
对于POST
方法中 url 中的发送参数,您可以简单地将其附加到 url 中,如下所示:
$.ajax({
type: 'POST',
url: 'superman?' + jQuery.param({ f1: "hello1", f2 : "hello2"}),
// ...
});