ajax,html,aspx,Net WebForm jQuery Ajax 传值到aspx后台

本文介绍了如何在ASP.NET的NetWebForm中通过jQuery实现Ajax调用后台定义的WebMethod。重点讲解了如何避免Page_Load拦截,分别展示了带参数的GET和POST请求,以及无参数操作和返回List的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在Net WebForm中,编写aspx文件,有时候想在后台编写类似WebAPI形式的方法。前台使用jQuery Ajax方式调用。【PS:jQuery ajax Get方式将直接走后台Page_Load方法,到不了标记的处理方法。】运行效果:

界面:

返回值:

2.前台代码

Inherits="WebApplication1.JQueryWebMethod" %>

jQuery ajax GET POST 到后台方法

function onGetAjax() {

/********************************/

window.alert('jquery ajax get方式无法直接调用aspx.cs后台方法!!!谨记!!!');

return;

/********************************/

$.ajax({

type: 'get',

url: 'JQueryWebMethod.aspx/GetAjax?a=121&b=122',

contentType: 'application/x-www-form-urlencoded;charset=utf-8', //请求头格式,key/value

dataType: 'json', //返回值格式,json

success: function (data) {

var jsonObj = JSON.parse(data.d);

console.log('get:a=' + jsonObj["a"] + ',b=' + jsonObj["b"]);

},

error: function (data) {

console.log(data);

}

});

};

/***************带参******************/

function onPostAjax(obj) {

$.ajax({

type: 'post',

url: 'JQueryWebMethod.aspx/PostAjax',

contentType: 'application/json;charset=utf-8', //请求头格式,json

dataType: 'json', //返回值格式,json

data: "{'a':'0','b':'1'}",

beforeSend: function () {

//禁用按钮,加遮罩层等

$(obj).attr('disabled', 'disabled');

},

success: function (data) {

var jsonObj = JSON.parse(data.d);

console.log('post:a=' + jsonObj["a"] + ',b=' + jsonObj["b"]);

},

error: function (data) {

var errMsg = data.responseJSON.Message;

console.log(errMsg);

},

complete: function () {

//启用按钮,取消遮罩层等

$(obj).removeAttr('disabled');

}

});

};

/***************无参******************/

function onPostAjax1(obj) {

$.ajax({

type: 'post',

url: 'JQueryWebMethod.aspx/PostAjax1',

contentType: 'application/json;charset=utf-8', //请求头格式,json

dataType: 'json', //返回值格式,json

beforeSend: function () {

//禁用按钮,加遮罩层等

$(obj).attr('disabled', 'disabled');

},

success: function (data) {

var jsonObj = JSON.parse(data.d);

console.log('post:a=' + jsonObj["a"] + ',b=' + jsonObj["b"]);

},

error: function (data) {

var errMsg = data.responseJSON.Message;

console.log(errMsg);

},

complete: function () {

//启用按钮,取消遮罩层等

$(obj).removeAttr('disabled');

}

});

};

/***************带参返回List******************/

function onPostAjax2(obj) {

$.ajax({

type: 'post',

url: 'JQueryWebMethod.aspx/PostAjax2',

contentType: 'application/json;charset=utf-8', //请求头格式,json

dataType: 'json', //返回值格式,json

data: "{'a':'值1','b':'值2'}",

beforeSend: function () {

//禁用按钮,加遮罩层等

$(obj).attr('disabled', 'disabled');

},

success: function (data) {

var jsonObj = data.d;

$.each(jsonObj, function (index, value) {

console.log('post:index=' + index + ',value=' + value);

});

},

error: function (data) {

var errMsg = data.responseJSON.Message;

console.log(errMsg);

},

complete: function () {

//启用按钮,取消遮罩层等

$(obj).removeAttr('disabled');

}

});

};

3.后台代码

先引用System.Web.Services;

然后代码。

public partial class JQueryWebMethod : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

///

/// 不可直接被前端Get调用

///

///

///

///

[WebMethod]

public static string GetAjax(string a,string b)

{

return "{\"a\":\""+a+"\",\"b\":\""+b+"\"}";

}

///

/// AJAX POST可用 【带参,返回json字符串】

///

///

///

///

[WebMethod]

public static string PostAjax(string a, string b)

{

return "{\"a\":\"" + a + "\",\"b\":\"" + b + "\"}";

}

///

/// AJAX POST可用 【无参】

///

///

[WebMethod]

public static string PostAjax1()

{

return "{\"a\":\"返回值1\",\"b\":\"返回值2\"}";

}

///

/// AJAX POST可用 【带参,返回List】

///

///

///

///

[WebMethod]

public static List PostAjax2(string a, string b)

{

return new List(){a,b,"值3","值4"};

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值