ajax中post怎么传参数类型,如何在$ ajax POST中传递参数?

9 个答案:

答案 0 :(得分:113)

我建议您在简单的情况下使用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以及另外一个参数。

$.post('superman', { field1: "hello", field2 : "hello2"},

function(returnedData){

console.log(returnedData);

}, 'json');

答案 1 :(得分:57)

尝试使用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.fail()和jqXHR.always()代替。

答案 2 :(得分:42)

Jquery.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时观察到的行为。

答案 3 :(得分:14)

function FillData() {

var param = $("#").val();

$("#tbDetails").append("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");

}

});

}

答案 4 :(得分:11)

在POST request中,参数会在请求正文中发送,这就是您在网址中看不到它们的原因。

如果您想看到它们,请更改

type: 'POST',

type: 'GET',

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

答案 5 :(得分:6)

$.ajax(

{

type: 'post',

url: 'superman',

data: {

"field1": "hello",

"field2": "hello1"

},

success: function (response) {

alert("Success !!");

},

error: function () {

alert("Error !!");

}

}

);

type: 'POST',会将**参数附加到请求的正文** 网址中未见在type: 'GET'时,将参数附加到可见的网址。

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

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

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

$.post('superman',

{

'field1': 'hello',

'field2': 'hello1'

},

function (response) {

alert("Success !");

}

);

答案 6 :(得分:6)

您可以使用$ .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);

}

);

答案 7 :(得分:3)

您的代码是正确的,除非您没有将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");

}

}

);

答案 8 :(得分:-3)

对于POST方法中的网址发送参数您只需将其附加到网址:

$.ajax({

type: 'POST',

url: 'superman?' + jQuery.param({ f1: "hello1", f2 : "hello2"}),

// ...

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值