警报消息在 Ajax Web 方法中无法正常工作

alert message not working properly in Ajax web method

本文关键字:常工作 工作 消息 Ajax 方法 Web      更新时间:2023-09-26

当我调用Ajax Web方法时,总是收到"保存错误"消息。但是我可以将数据保存到数据库。我找不到任何错误。如何获得警报"已保存"。我的网络方法是

$.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            url: "paygrade.aspx/SavePayGrade",
            data: JSON.stringify({ value: rec, arr: arr }),
            success: function (msg) {
                console.log(msg.d);
                alert(msg.d);
            },
            error: function (msg) {
                alert("Save error");
            }
        });

 [WebMethod]
  public static string SavePayGrade(string value, params string[] arr)
    {
        string res = "";
        using (FlairEntities context = new FlairEntities())
        {
            tblPayGrade obj = new tblPayGrade();
                obj.salaryGrade = arr[0];
                obj.salary = arr[1];
                obj.note = arr[2];
                context.tblPayGrades.Add(obj);
                context.SaveChanges();
                res = "Saved";
        }
        return res;
    }

请尝试以下操作

console.log(msg);
alert(msg);

当您以字符串(返回 res;)发送响应时,您不能使用 msg.d您只需要使用味精