无法使用ajax调用将值插入数据库

时间:2015-10-11 09:44:52

标签: c# jquery ajax web-services soap

我不知道为什么在调用ajax方法时出现错误。网络服务工作正常。我已托管在我的本地机器上并进行了检查。非常感谢任何帮助。

网络服务代码:

public class Contacts : System.Web.Services.WebService
{

    [WebMethod]
    public void InsertIntoContacts(string name,string email,string message)
    {

        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["KarthikDBConnString"].ConnectionString;
        MySqlConnection connection = new MySqlConnection(connectionString);
        MySqlCommand cmd = new MySqlCommand("Insert into contacts values(@name , @email , @message)");
        cmd.Connection = connection;
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@email", email);
        cmd.Parameters.AddWithValue("@message", message);
        connection.Open();
        cmd.ExecuteNonQuery();
        connection.Close();
    }
}

表单的html标记是:

<form role="form" id="contactForm">
<div class="form-group">
    <label for="name">Name :</label>
    <input type="text" class="form-control" placeholder="Enter your Name" id="name" />
</div>
<div class="form-group">
    <label for="email">Email :</label>
    <input type="email" class="form-control" placeholder="Enter your Email here (optional)" id="email" />
</div>
<div class="form-group">
    <label for="message">Message :</label>
    <textarea class="form-control" rows="3" placeholder="Enter your message here" id="message"></textarea>
</div>
<div class="form-group">
    <button type="button" class="btn btn-primary" id="sendMsgBtn" onclick="javascript:SendMsg()">Send Message</button>
</div>
</form>

ajax调用是:

    function SendMsg() {
    var soapRequset = '<?xml version="1.0" encoding="utf-8"?>\
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
  <soap:Body>\
    <InsertIntoContacts xmlns="http://tempuri.org/">\
      <name>'+$("#name").val()+'</name>\
      <email>'+$("#email").val()+'</email>\
      <message>'+$("#message").val()+'</message>\
    </InsertIntoContacts>\
  </soap:Body>\
</soap:Envelope>';

    $.ajax({
        url: "host/contacts.asmx",
        type: "POST",
        data: soapRequset,
        contentType: "text/xml; charset=utf-8",
        dataType: "xml",
        processData: false,
        success: function (xData, status) { alert(status) },
        error: function (xData, status, req) {
              alert(xData.status);
              alert(status);
              alert(req);
    }
    });
}

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

经过大量研究,我发现ajax错误代码0意味着jQuery无法访问网址。所以我在同一个网站上托管了Web服务。现在它的工作。修改后的ajax调用如下:

function sendMsg() {
var soapRequest = '<?xml version="1.0" encoding="utf-8"?>\
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
        <soap:Body>\
          <InsertIntoContacts xmlns="http://tempuri.org/">\
            <name>'+ $("#name").val() + '</name>\
            <email>' + $("#email").val() + '</email>\
            <message>' + $("#message").val() + '</message>\
         </InsertIntoContacts>\
       </soap:Body>\
   </soap:Envelope>';
$.ajax({
    url: "../WebService/contacts.asmx",
    type: "POST",
    data: soapRequest,
    contentType: "text/xml; charset=utf-8",
    dataType: "xml",
    processData: false,
    success: function (xData, status) { alert(status) },
    error: function (xData, status) { alert(status); }
});
}