做非法网站,东莞专业网站推广方式,asp.net 网站 方案,泰安网络设计公司今天了解了asp.net下使用记录下使用ajax的两种方式。当然并不是说只有这两种方式。这里仅记录这两种方式。
用.ashx文件来处理前端发送过来的ajax请求#xff1a; demo如下#xff1a; 前端代码#xff1a; script typetext/javascript$(func…今天了解了asp.net下使用记录下使用ajax的两种方式。当然并不是说只有这两种方式。这里仅记录这两种方式。
用.ashx文件来处理前端发送过来的ajax请求 demo如下 前端代码 script typetext/javascript$(function () {$(#MyButton).click(function () {$.ajax({type: post,url: MyTest.ashx,data:{dataa:ashx文件处理的结果},success: function (data) {alert(data);},error: function (err) {alert(12313123);}});});});/script后端代码 //MyTest.ashx
public class MyTest : IHttpHandler, IRequiresSessionState{public void ProcessRequest(HttpContext context){context.Response.ContentType text/plain;string ts context.Request.Form[dataa];context.Response.Write(ts);}public bool IsReusable{get{return false;}}} 结果 方法二用.aspx文件中的方法来处理前端请求
前端代码 script typetext/javascript$(function () {$(#MyButton).click(function () {$.ajax({type: post,url: test.aspx/SayHello,data: {dataa:aspx文件处理},//注意两种方法的json格式不一样datatype: json,contentType: application/json;charsetutf-8,success: function (data) {data JSON.parse(data);alert(data.d);},error: function (err) {alert(12313123);}});});});/script后端代码 using System.Web.Services;[WebMethod]public static string SayHello(string dataa){return dataa;} 结果 注意要点 两种方法的data属性里JSON写法不能混用。
参考文章
1.JS操作JSON总结
2.Jquery Ajax调用aspx页面方法
3.用Ajax提交数据到ashx处理数据