简体   繁体   English

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

[英]Unable to insert values into database using ajax call

I don't know why I am getting an error when I am calling the ajax method . 我不知道为什么在调用ajax方法时出现错误。 The web service is working fine . 网络服务工作正常。 I have hosted in my local machine and checked . 我已托管在我的本地机器上并进行了检查。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Web Service Code: 网络服务代码:

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();
    }
}

The html markup for the form is: 表单的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>

And the ajax call is : 而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);
    }
    });
}

After lots of research I found that ajax error code 0 means that jQuery is unable to reach the url . 经过大量研究后,我发现ajax错误代码0意味着jQuery无法访问url。 So I hosted the web service in the same web site . 所以我在同一个网站上托管了Web服务。 Now its working. 现在它的工作。 The modified ajax call is below: 修改后的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); }
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM