Ajax调用asp.net POST方法
本文关键字:POST 方法 net asp 调用 Ajax | 更新日期: 2023-09-27 18:11:40
最近我在调用ASP时遇到了一个问题。NET POST Web方法从jQuery AJAX。但是,每次运行这个程序时,AJAX调用都会响应error: undefined
var departmentBO = {
dept_id: "",
dept_name: "",
msg: "",
isException: ""
};
function updateDepartment() {
$.ajax({
type: 'POST',
dataType: 'json',
async: false,
data: JSON.stringify(departmentBO),
url: 'AdminPanel.aspx/UpdateDepartmentNameJSON',
cache: false,
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert('Department Name Updated');
},
error: function (xhr, err) {
alert(xhr.responsetext);
}
});
}
[WebMethod]
public static string UpdateDepartmentNameJSON(DepartmentsBO departmentBO)
{
string jsonOutput = null;
try
{
jsonOutput = JsonConvert.SerializeObject("It is Working");
}
catch (Exception ex)
{
}
return jsonOutput;
}
部门BO类:
public class DepartmentsBO
{
public string dept_id { get; set; }
public string dept_name { get; set; }
public string msg { get; set; }
public Boolean isException { get; set; }
}
我错过了什么吗?你能帮我解决这个问题吗?
尝试用
替换数据data: "{departmentBO : " + JSON.stringify(departmentBO) + "}",
我只是像这样改变你的变量,它开始工作了
var departmentBO = {
"dept_id" : "",
"dept_name" : "",
"msg" : "",
"isException" : ""
};
也没有任何影响删除async:false
在尝试了几种实际解决问题的方法之后,我更改了AJAX参数,如下所示:
$.ajax({
type: 'POST',
dataType: 'json',
data: JSON.stringify({ "departmentBO": departmentBO }),
url: 'AdminPanel.aspx/UpdateDepartmentNameJSON',
cache: false,
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert('Department Name Updated');
},
error: function (xhr, err) {
alert(xhr.responsetext);
}
});