2017-08-31 144 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 +'「}',

謝謝。

相關問題