如何在 $ajax POST 中传递参数?

我已经按照教程中说明的 这个链接。在下面的代码中,由于某种原因,数据没有作为参数附加到 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");
}
});
850407 次浏览

POST 请求中,参数在请求的主体中发送,这就是为什么在 URL 中看不到它们的原因。

如果你想看,就换衣服

    type: 'POST',

    type: 'GET',

注意,浏览器有一些开发工具,可以让您查看代码发出的完整请求。在 Chrome 中,它在“ Network”面板中。

对于简单的情况,我建议您使用 jQuery 的 $.post$.get语法:

$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
});

如果您需要捕获失败案例,只需这样做:

$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
}).fail(function(){
console.log("error");
});

此外,如果总是发送 JSON 字符串,则可以使用 $. getJSON或 $。在最后多加一个参数。

$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
}, 'json');

尝试使用 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
});

使用 POST 方法无法在 URL 中看到参数。

编辑:

弃用通知: jqXHR.Success ()、 jqXHR.error ()和 从 jQuery 3.0开始删除 jqXHR.complete ()回调 而是 jqXHR.done ()、 jqXHR.fall ()和 jqXHR.always ()。

    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");
}
});
}

你可以使用 $. ajax 或者 $. post

使用 $. ajax:

    $.ajax({
type: 'post',
url: 'superman',
data: {
'field1': 'hello',
'field2': 'hello1'
},
success: function (response) {
alert(response.status);
},
error: function () {
alert("error");
}
});

使用 $. post:

    $.post('superman',
{
'field1': 'hello',
'field2': 'hello1'
},
function (response, status) {
alert(response.status);
}
);

Ajax 不像对 GET 数据那样自动为您编码 POST 数据。Jquery 希望您的数据被预先格式化,以附加到要直接通过网络发送的请求主体中。

一种解决方案是使用 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");
}
});

在这种情况下,param方法将数据格式化为:

field1=hello&field2=hello2

Jquery.ajax文档说,有一个名为 processData的标志,它控制这种编码是否自动完成。文档说明它默认为 true,但是当使用 POST时,我观察到的行为不是这样的。

除了没有将 JSON 键作为字符串传递之外,您的代码是正确的。

它周围应该有双引号或单引号

{“ 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");
}
}
);

POST方法中,如果要在 url 中使用 send 参数,您可以像下面这样简单地将它附加到 url:

$.ajax({
type: 'POST',
url: 'superman?' + jQuery.param({ f1: "hello1", f2 : "hello2"}),
// ...
});
$.ajax(
{
type: 'post',
url: 'superman',
data: {
"field1": "hello",
"field2": "hello1"
},
success: function (response) {
alert("Success !!");
},
error: function () {
alert("Error !!");
}
}
);

type: 'POST'将把 * * 参数 附加到请求 * * 的正文中,这个请求在 网址中是 没见过,而在 type: 'GET'中,将参数附加到 URL 中,这个 URL 是 看得见

大多数流行的 Web 浏览器都包含显示 完整的请求的网络面板。

在网络面板中选择 XHR查看 请求

这也可以通过这个来完成。

$.post('superman',
{
'field1': 'hello',
'field2': 'hello1'
},
function (response) {
alert("Success !");
}
);
function funcion(y) {
$.ajax({
type: 'POST',
url: '/ruta',
data: {"x": y},
contentType: "application/x-www-form-urlencoded;charset=utf8",
});
}

我知道现在回答这个问题已经很晚了

还可以使用 FormData 将数据传递到 $.ajax({})

let formData =  new FormData()
formData.append('data1', "Hello")
formData.append('data2', "World")


$.ajax({
url: '/',
type: 'POST',
data: formData,
contentType: false,
processData: false,
cache: false,
success: function(v){
console.log(v)
}
})

问题在于参数传递的方式

这个密码对我很管用

 data: jQuery.param({ DistrictId: 5, TalukId: 0, HobliId: 0 }),
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',