2017-08-31 111 views
1

我写的webservice插入数据到我的数据库和webservice工程时,我使用url访问服务"http:\\localhost\Webservice.asmx"。如果我使用jquery ajax调用相同的url它返回(500)内部服务器。这是我的源代码。使用jQuery调用Web服务返回500内部服务器错误?

//代码中插入客户详细信息

$('#submitAddDetail').on('click', function (e) { 

     //alert('Hello World'); 
     var custId = $(".addcustomer-body #custId").val(); 
     var custName = $(".addcustomer-body #custName").val(); 
     var custCity = $(".addcustomer-body #custCity").val(); 
     var custPostalCode = $(".addcustomer-body #custPostalCode").val(); 
     var custCountry = $(".addcustomer-body #custCountry").val(); 
     var custPhone = $(".addcustomer-body #custPhone").val(); 
     var custFax = $(".addcustomer-body #custFax").val(); 
     debugger; 
     //alert(custName); 
     $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      url: "WebService.asmx/AddCustomers", 
      data: "{ 'sCustomerId': '" + custId + "','sCompanyName': '" + custName + "','sCity':'" + custCity + "','sPostalCode':'" + custPostalCode + "','sCountry':'" + custCountry + "','sPhone:'" + custPhone + "','sFax':'" + custFax + "'}",     
      success: function (data) { 
       debugger; 
       if (data.d == 'true') { 
        location.reload(); 
       } 
       else { 
        debugger; 
        alert('Error in Inserting data'); 
       } 
      } 
     }); 
    }); 

//我的web服务,以插入客户数据。 [的WebMethod]

public string AddCustomers(string sCustomerId, string sCompanyName, string sCity, string sPostalCode, string sCountry, string sPhone, string sFax) 
    { 
     string msg = string.Empty; 
     string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(constr)) 
     { 
      string sQuery = "INSERT INTO Customers (CustomerID,CompanyName,City,PostalCode,Country,Phone,Fax)VALUES" + 
          "(@CustomerID,@CompanyName,@City,@PostalCode,@Country,@Phone,@Fax)"; 
      using (SqlCommand cmd = new SqlCommand(sQuery)) 
      { 
       cmd.Connection = con; 
       cmd.Parameters.AddWithValue("@CustomerID", sCustomerId); 
       cmd.Parameters.AddWithValue("@CompanyName", sCompanyName); 
       cmd.Parameters.AddWithValue("@City", sCity); 
       cmd.Parameters.AddWithValue("@PostalCode", sPostalCode); 
       cmd.Parameters.AddWithValue("@Country", sCountry); 
       cmd.Parameters.AddWithValue("@Phone", sPhone); 
       cmd.Parameters.AddWithValue("@Fax", sFax); 
       con.Open(); 
       int i = cmd.ExecuteNonQuery(); 
       if (i == 1) 
       { 
        msg = "true"; 
       } 
       else 
       { 
        msg = "false"; 
       } 
      } 
     } 
     return msg; 
    }//End 

我需要你提前解决这一issue.Thanks帮助。

回答

0

我改变了JQuery语法的一行,它的工作。

data:'{“sCustomerId”:“'+ custId +'”,“sCompanyName”:“'+ custName +'”,“sCity”:“'+ custCity +'”,“sPostalCode” + custPostalCode +'“,”sCountry“:”'+ custCountry +'“,”sPhone“:”'+ custPhone +'“,”sFax“:”'+ custFax +'“}',

谢谢。

相关问题