3

我正在尝试将数据从 webform 传递到方法背后的代码并在 webform 中取回值,然后打印它。我最初测试了以下代码以简单地将请求发布到方法,获取字符串并在页面中打印并且它有效,但是在尝试将数据发布回方法时出现问题

$(document).ready(function () {

$(".AddStaffToRoleLink").on("click", function () {

           var selectedStaffID = $(this).attr("id");

           alert("this is " + selectedStaffID);

           $.ajax({

               type: "POST",
               url: "AddUserInRole.aspx/AddRoleForSelectStaff",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               data: { selectedStaffID: selectedStaffID },
               success: function (response) {
                   $("#Content").text(response.d);
               },
               failure: function (response) {
                   alert(response.d);
               }
           });
       });

});

背后的代码

   [WebMethod]
    public static string AddRoleForSelectStaff(string selectedStaffID)
    {
        return "This string is from Code behind  " + selectedStaffID;
    }
4

3 回答 3

7

这是将单一数据发布到方法背后的网络表单代码的方法......

  $(document).ready(function () {

 $(".AddStaffToRoleLink").on("click", function () {

           var selectedStaffID = $(this).attr("id");

           alert("this is " + selectedStaffID);

           $.ajax({
               url: 'AddUserInRole.aspx/AddRoleForSelectStaff',
               type: "POST",
               data: "{'GivenStaffID':'" + selectedStaffID +"'}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (response) {
                   $("#Content").text(response.d);
               },
               failure: function (response) {
                   alert(response.d);
               }
           }).done(function (response) {
               alert("done "+response );
           });
       });
   });

代码隐藏方法

 [WebMethod]
    public static string AddRoleForSelectStaff(string GivenStaffID)
    {
        var staffID = Convert.ToInt32(GivenStaffID);

        return "This string is from Code behind  " + GivenStaffID;
    }
于 2015-09-22T11:58:52.177 回答
1
 function GetEmployee()
    {
        var jsonObj = {"Org":0 ,"Dept":0,"Desg":0,"Grp":0,"SubGrp":0,"Loc":0,"Prv":'CanViewEmployee',"CustAttrId":0,CustAttrVal:"","Status":'1' };
        $.ajax({
            url: 'EmployeeSearchControl.aspx/populateJsonResult',
            datatype: 'json',
            data:JSON.stringify(jsonObj),
            method: 'Post',
            success: function (data) {
                if(data != null || data != "")
                {
                    for(var i=0;i<data.length;i++)
                    {
                        addEmployeeToGrid(data[i]);
                    }
                }
            }
        })
    }


 public class JSONRequest
    {
        public long Org { get; set; }
        public long Dept { get; set; }
        public long Desg{ get; set; }
        public long Grp { get; set; }
        public long SubGrp { get; set; }
        public long Loc { get; set; }
        public long Cat { get; set; }
        public string Prv { get; set; }
        public long CustAttrId { get; set; }
        public string CustAttrVal { get; set; }
        public string Status { get; set; }
    }
protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "application/json";
            string ap = Request["org"];
            HttpContext.Current.Request.InputStream.Position = 0;
            string jsonString = "";
            using (StreamReader inputStream = new StreamReader(this.Request.InputStream))
            {
                jsonString = inputStream.ReadToEnd();
            }
            JSONRequest masterDetails = Newtonsoft.Json.JsonConvert.DeserializeObject<JSONRequest>(jsonString);
}
于 2016-10-12T05:20:05.657 回答
1

代码:

protected void Page_Load(object sender, EventArgs e)
    { 
HttpContext.Current.Request.InputStream.Position = 0;
        string jsonString = "";
        using (StreamReader inputStream = new     StreamReader(this.Request.InputStream))
        {
            jsonString = inputStream.ReadToEnd();
        }
        JSONRequest masterDetails =   Newtonsoft.Json.JsonConvert.DeserializeObject<JSONRequest>(jsonString);
 }

public class JSONRequest
{
    public long Org { get; set; }
    public long Dept { get; set; }
    public long Desg{ get; set; }
    public long Grp { get; set; }
    public long SubGrp { get; set; }
    public long Loc { get; set; }
    public long Cat { get; set; }
    public string Prv { get; set; }
    public long CustAttrId { get; set; }
    public string CustAttrVal { get; set; }
    public string Status { get; set; }
}

function GetEmployee()
{
    var jsonObj = {"Org":0,"Dept":0,"Desg":0,"Grp":0,"SubGrp":0,"Loc":0,"Prv":'CanViewEmployee',"CustAttrI    d":0,CustAttrVal:"","Status":'1' };
    $.ajax({
        url: 'EmployeeSearchControl.aspx/populateJsonResult',
        datatype: 'json',
        data:JSON.stringify(jsonObj),
        method: 'Post',
        success: function (data) {
            if(data != null || data != "")
            {
                for(var i=0;i<data.length;i++)
                {
                    addEmployeeToGrid(data[i]);
                }
            }
        }
    })
}
于 2016-10-12T05:13:39.653 回答