Jquery ajax提交数据内部服务器错误

时间:2014-09-09 10:03:34

标签: jquery asp.net ajax

我从internal server error:500获得了webservice。你能帮我找出原因吗?

$(document).ready(function () {
            $("#btnSubmit").click(InsertRegistration);
        });

        function InsertRegistration() {
            var Name = $("#Text1").val;
            var Designation = $("#Text2").val;
            var Address = $("#Text3").val;

            $.ajax(
                {
                    url: "Web/Employee.asmx/StoreEmployee",
                    type: "POST",
                    contenType: "application/json; charset=utf-8"",
                    dataType: "json",
                    data: "{'Name' : '" + Name + "','Designation':'" + Designation + "','Address':'" + Address + "'}",

                    success: function (result) {
                        var div = $("#divMessage");
                        div.InnerHtml = result.d;
                        div.html(result.d).fadeIn(1000);
                        setTimeout(function () { div.fadeOut(1000) }, 5000);

                    },
                    error: function (xhr, status) {
                        alert("An error occured " + status);
                    }

                }
            );
        }

我的Web服务如下所示我已经为该类添加了脚本服务和[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

[ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Employee : System.Web.Services.WebService {

    SqlConnection conn = new SqlConnection(
            "Data Source=ADMN-PC;Initial Catalog=demoDb;Integrated Security=true");

    public Employee () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    string Name , Designation , Address;

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string StoreEmployee(string Name, string Designation, string Address)
    {
        this.Name = Name;
        this.Designation = Designation;
        this.Address = Address;

        if (InsertEmployeeDB() > 0)
        {
            return "Saved Succesfully";
        }
        else
        {
            return "Error occured while saving...";
        }


    }
    public int InsertEmployeeDB()
    {
        try
        {

            SqlCommand cmd = new SqlCommand("insert into  tblEmp_Personal (Name ,Designation,Address) values ('" + Name + "','" + Designation + "','" + Address + "')",conn);
            conn.Open();
            return cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {

        }

        return 0;
    }

}

2 个答案:

答案 0 :(得分:0)

ajax中的数据部分将创建无效的json字符串。像这样更改data

data: JSON.stringify({
    Name: Name,
    Designation: Designation,
    Address: Address
}),

然后你的ajax会是这样的,

 $.ajax({
     url: "Web/Employee.asmx/StoreEmployee",
     type: "POST",
     contenType: "application/json; charset=utf-8",
     dataType: "json",
     data: JSON.stringify({
         Name: Name,
         Designation: Designation,
         Address: Address
     }),

     success: function(result) {
         var div = $("#divMessage");
         div.InnerHtml = result.d;
         div.html(result.d).fadeIn(1000);
         setTimeout(function() {
             div.fadeOut(1000)
         }, 5000);

     },
     error: function(xhr, status) {
         alert("
             An error occured " + status);
     }

 });

现在我遇到了你的问题。在webservice代码的顶部,有一个这样的注释行,

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]

取消注释第[System.Web.Script.Services.ScriptService]

答案 1 :(得分:0)

用此替换您的代码(仅限我已更改的部分)

url: "Web/Employee.asmx/StoreEmployee",
                type: "POST",
                contenType: "application/json; charset=utf-8",
                dataType: "json",
                data: "{'Name' : '" + Name + "','Designation':'" + Designation + "','Address':'" + Address + "'}",

                success: function (result) {
                    var div = $("#divMessage");
                    div.InnerHtml = result.d;
                    div.html(result.d).fadeIn(1000);
                    setTimeout(function () { div.fadeOut(1000) }, 5000);

                },

你最后在内容类型中放了两次双引号。试试这个。