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

这篇博客介绍了如何使用jQuery进行Ajax请求,特别是$.post和$.get方法。文章详细解释了如何处理成功和失败的情况,并强调了在POST请求中参数不会显示在URL中。此外,还提到了在发送JSON数据时使用$.getJSON和$.ajax的注意事项,以及如何处理POST数据的编码问题。最后,提到了浏览器的开发者工具可以用来查看请求详情。
摘要由CSDN通过智能技术生成

I would recommend you to make use of the $.post or $.get syntax of jQuery for simple cases:

$.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 $.getJSON or $.post with one more parameter at the very end.

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

function(returnedData){

console.log(returnedData);

}, 'json');

Try using GET method,

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.

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.

Jquery.ajax does 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.

A solution is to use the jQuery.param function to build a query string that most scripts that process POST requests expect.

$.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:

field1=hello&field2=hello2

The Jquery.ajax documentation 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.

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

}

});

}

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.

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.

$.ajax(

{

type: 'post',

url: 'superman',

data: {

"field1": "hello",

"field2": "hello1"

},

success: function (response) {

alert("Success !!");

},

error: function () {

alert("Error !!");

}

}

);

type: 'POST', will append **parameters to the body of the request** which is not seen in the URL while type: 'GET', appends parameters to the URL which is visible.

Most of the popular web browsers contain network panels which displays the complete request.

In network panel select XHR to see requests.

This can also be done via this.

$.post('superman',

{

'field1': 'hello',

'field2': 'hello1'

},

function (response) {

alert("Success !");

}

);

You can do it using $.ajax or $.post

Using $.ajax :

$.ajax({

type: 'post',

url: 'superman',

data: {

'field1': 'hello',

'field2': 'hello1'

},

success: function (response) {

alert(response.status);

},

error: function () {

alert("error");

}

});

Using $.post :

$.post('superman',

{

'field1': 'hello',

'field2': 'hello1'

},

function (response, status) {

alert(response.status);

}

);

Your code was right except you are not passing the JSON keys as strings.

It should have double or single quotes around it

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

}

}

);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值