Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 . 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:

<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 :

    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);
    }
    });
}
like image 519
Karthik Avatar asked May 30 '26 03:05

Karthik


1 Answers

check this out using jquery to directly call aspnet ajax page methods

like image 126
OMID Avatar answered May 31 '26 17:05

OMID