Ajax调用asp.net POST方法

时间:2016-10-06 06:57:28

标签: javascript c# jquery asp.net ajax

最近我在从jQuery AJAX调用ASP.NET POST Web方法时遇到了问题。但是,每次运行此程序时,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; }
}

我有什么遗漏的吗?在这种情况下你能帮我解决吗?

3 个答案:

答案 0 :(得分:0)

尝试用

替换数据
data: "{departmentBO : " + JSON.stringify(departmentBO) + "}",

答案 1 :(得分:0)

我只是像这样改变你的变量并开始工作

var departmentBO = {
                "dept_id" : "",
                "dept_name" : "",
                "msg" : "",
                "isException" : ""
            };

删除async:false

也没有任何影响

答案 2 :(得分:0)

在尝试了几个实际解决了我的问题的方法之后,我改变了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);
                }
            });