標籤:
在做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
jQuery常用ajax操作