Tox*_*xic 2 webforms asp.net-3.5 jquery-ajaxq
我试图将数据从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);
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
[WebMethod]
public static string AddRoleForSelectStaff(string selectedStaffID)
{
return "This string is from Code behind " + selectedStaffID;
}
Run Code Online (Sandbox Code Playgroud)
这里是将sigle数据发布到方法后面的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 );
});
});
});
Run Code Online (Sandbox Code Playgroud)
[WebMethod]
public static string AddRoleForSelectStaff(string GivenStaffID)
{
var staffID = Convert.ToInt32(GivenStaffID);
return "This string is from Code behind " + GivenStaffID;
}
Run Code Online (Sandbox Code Playgroud)