gpt4 book ai didi

webforms - jQuery $Ajax 将数据从 webform 发布到 ASP.NET 中的代码隐藏方法

转载 作者:行者123 更新时间:2023-12-02 06:32:05 27 4
gpt4 key购买 nike

我正在尝试将数据从 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;
}

最佳答案

这是在方法后面将单一数据发布到 webform 代码的方法...

  $(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;
}

关于webforms - jQuery $Ajax 将数据从 webform 发布到 ASP.NET 中的代码隐藏方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32714605/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com