[笔记系列] jQuery:简易的Ajax与JSON参数传值

//传至后端之资料用Json包起来        var sJson = JSON.stringify({ sCustId: cid, sActivityId: activitVal});$.ajax({    type: "POST",    //async为false -> 同步     //async为true  -> 非同步    async: false,       dataType: "json",    url: "从根目录开始或者相对路径等等/指到的.aspx/function_01",    //↑↑↑↑↑↑反正就是要指到你写的aspx拉↑↑↑↑↑↑↑↑    contentType: 'application/json; charset=UTF-8',    data: sJson,    success: function (msg) {        //后端回传的东西包成Object回来,        //ex:{sResult = 例如在后端自己命名的某型别sResult物件}        var sResult = msg.d.sResult;        //后端回传的东西包成Object回来,        //ex:{db_Result = 例如在后端自己命名的DataTable物件}        var db_Result = msg.d.db_Result;                //do something        },        //statusCode範例        statusCode: {            403: function (response) {                LocationHerf();            }        }    }});var LocationHerf = function () {    window.location.href = "你要转page的URL拉";};//----------------------------------------------------//后端程式码範例 -- 中间内容不重要 主要看return包了什么东西[WebMethod(EnableSession = true)]public static object function_01(string sCustId, string sActivityId){    //判断Session    if (HttpContext.Current.Session["UserSession"] == null)    {        HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.Forbidden;    }    string sResult = string.Empty;    string db_Result = string.Empty;    string sql = string.Empty;    Dictionary<string, object> dic = new Dictionary<string, object>();    try    {        sql = @"SELECT * FROM ActivityRecord                 WHERE CustomerId = @CustomerId                AND ActivityId = @ActivityId";        dic.Clear();        dic.Add("@CustomerId", sCustId);        dic.Add("@ActivityId", sActivityId);        DataHelper dh = new DataHelper();        DataTable dt = dh.queryData(sql, dic);        int dbCount = dt.Rows.Count;        //若资料库找不到纪录 表示 没有重複        if (dbCount == 0)        {            sResult = "success";            //不重要 反正就是去call我另一个function去做事就是了 回传DataTable            db_Result = AddDbActivityRecord(sCustId, sActivityId);        }        else        {            sResult = "replay";        }    }    catch (Exception ex)    {        sResult = ex.ToString();    }    finally    {        // 关闭连接        SqlCommand cmd = new SqlCommand();        cmd.Connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["conStr"].ConnectionString.ToString());        SqlDataAdapter da = new SqlDataAdapter(cmd);        dbTool.CloseDB(cmd, da);    }    return new    {        db_Result = db_Result,        sResult = sResult    };}

若有错误请各路大神指教
纯属笔记笔记


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章