在做asp.net项目的时候经常会用到ajax操作,现总结常用的ajax操作供平时项目中参考
第一种:
前端代码:
<script type="text/javascript"> $.post("../../tools/GetItemData.ashx", { "canshu1": "参数1的值", "canshu2": "参数2的值" }, function (data) { //返回的是json格式的数据 }, "json"); </script>
后端代码:
public class ceshi : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string pagenumstr = context.Request["canshu1"]; string text = context.Request["canshu2"]; //取得数据FrameHtml context.Response.Write(new JavaScriptSerializer().Serialize(FrameHtml)); } }
说明:
if Request.QueryString("ID")==Null这是url栏没有这个ID变量
if Request.QueryString("ID").ToString()=="" 判断ID是否为空
第二种:
前端代码:
<script type="text/javascript"> //验证是否登录 $.ajax({ type: "POST", url: "{config.webpath}tools/submit_ajax.ashx?action=user_check_login", dataType: "json", timeout: 20000, success: function (data, textStatus) { if (data.status == 1) { } } }); </script>
后端代码:
public void ProcessRequest(HttpContext context) { //取得处事类型 string action = EazyRequest.GetQueryString("action"); switch (action) { case "user_check_login": //检查用户是否登录 user_check_login(context); break; case "applyspace": //更新申请 apply_user_space(context); break; } } #region 检查用户是否登录============================= private void user_check_login(HttpContext context) { //检查用户是否登录 Model.users model = new BasePage().GetUserInfo(); if (model == null) { context.Response.Write("{\"status\":0, \"username\":\"匿名用户\"}"); return; } context.Response.Write("{\"status\":1, \"username\":\"" + model.user_name + "\", \"userid\":\"" + model.id + "\", \"zuid\":\"" + model.group_id + "\"}"); } #endregion
时间: 2024-10-06 12:16:07