Fork me on GitHub
.net求学者

Ajax前台调用后台方法、AJAX Pro2(回调函数)

//获取分店
function cityResult() {
    if (cityName != "") {
        $("#ddlcity_").find("option[text='" + cityName + "']").attr("selected", true);
    }
    var city = $("#ddlcity_").val();
    if (city != "--请选择城市--") {
        AjaxMethod.GetBranchList(city, get_branch_Result_CallBack);
    }
    else {
        $("#ddbranch_").find("option").remove();
        document.all("ddbranch_").options.add(new Option("--请选择分店--", "0"));
        clearResult();
    }
}
//加载分店
function get_branch_Result_CallBack(response) {
    if (response.value != null) {
        document.all("ddbranch_").length = 0;
        var ds = response.value;
        document.all("ddbranch_").options.add(new Option("--请选择分店--", "0"));
        if (ds != null && typeof (ds) == "object" && ds.Tables != null) {
            for (i = 0; i < ds.Tables[0].Rows.length; i++) {
                var name = ds.Tables[0].Rows[i].bname;
                var id = ds.Tables[0].Rows[i].id + "$" + ds.Tables[0].Rows[i].bname + "$" + ds.Tables[0].Rows[i].substoreid;
                document.all("ddbranch_").options.add(new Option(name, id));
            }
            if (brandName != "") {
                $("#ddbranch_").find("option[text='" + brandName + "']").attr("selected", true);
            }
        }
    }
    return;
}
 public class AjaxMethod
    {
        BLL.Branch bBr = new BLL.Branch();

        [Ajax.AjaxMethod]
        //根据城市加载分店信息
        public DataSet GetBranchList(string city)
        {
            return bBr.GetListforcity(string.Format("{0}", city));
        }
}

AJAX Pro2包及其案例下载

 

 

方法二:

仅支持POST提交方法

/*.aspx 页面*/

$.ajax({
                type: 'POST',
                async: true,
                contentType: 'application/json',
                url: 'Discount_Edit.aspx/Submit_Ok',
                dataType: 'json',
                data: "{'data':'" + data + "'}",
                error: function (err) {
                    alert('出错了,错误信息:' + err + ',请联系技术员!');
                    return false;
                },
                success: function (result) {
                    if (result.d != "Ok") {
                        alert(result.d);
                    }
                    else {
                        alert("提交成功");
                        history.go(0);
                    }
                }
            });
/*.aspx.cs后台代码*/
  [System.Web.Services.WebMethod]
        public static string Submit_Ok(string data)
        {
            //其它操作
            Return “Ok”;
        }

 data: "{'data':'" + data + "'}",此处要注意为字符串json格式

 

 

posted @ 2014-07-03 10:37  hy31337  阅读(293)  评论(0)    收藏  举报
.net求学者