当我使用url "http:\\localhost\Webservice.asmx"
访问服务时,我编写了webservice来向我的数据库插入数据和webservice。如果我使用jquery ajax调用相同的url,它返回(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
我需要你的帮助来解决这个问题。谢谢。
答案 0 :(得分:0)
我在Jquery语法中更改了一行并且它有效。
data:'{“sCustomerId”:“'+ custId +'”,“sCompanyName”:“'+ custName +'”,“sCity”:“'+ custCity +'”,“sPostalCode”:“'+ custPostalCode +'“,”sCountry“:”'+ custCountry +'“,”sPhone“:”'+ custPhone +'“,”sFax“:”'+ custFax +'“}',
感谢。