Dinesh Chandra Dinesh Chandra - 5 months ago 8
Ajax Question

Onclick ajax is not working

When I click on submit button info of textarea tag should be sent to mail using ajax.can anyone helpme.thankyou.

$(document).on("click", "#submit-btn", function() {
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url:"https://ivr.callxl.com/callXLWeb/SendingEmail",
data: "comment=" +$("#cmessage").val(),
dataType: "json",
async: true,
success: function (data, textStatus, jqXHR) {
if(data.success)
{
alert("successfully sent");
}
else
{

}
},
error: function(jqXHR, textStatus, errorThrown){
alert(jqXHR.responseText);
console.log("Something really bad happened " + textStatus);
$("#errorResponse").html(jqXHR.responseText);
},

});
});

Answer

I think you should do it like this.

$("#submit-btn").on("click",function() {
    $.ajax({
        url: "https://ivr.callxl.com/callXLWeb/SendingEmail",
        type: 'POST',       
        contentType: "application/json; charset=utf-8",
        data: { comment: $("#cmessage").val() },
        dataType: "json",
        success: function (data, textStatus, jqXHR) {
            if (data.success) {
                alert("successfully sent");
            } else {
                // handle error here...
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert(jqXHR.responseText);
            console.log("Something really bad happened " + textStatus);
            $("#errorResponse").html(jqXHR.responseText);
        }
    });
});