I'm a complete beginner to this. I have searched endlessly over the internet but I just cannot figure out the problem. Probably I'm making some basic conceptual mistake, so any help would be appreciated.
I want to take a simple form input using javascript or jquery and an ajax request. Here's the relevant code.
//JavaScript:
function sendMails(){
console.log("This is executing");
var data={
first_name: $("#first_name").val(),
email: $("#email").val(),
toArtists: $("#toArtists").val(),
comments: $("#comments").val(),
bcc_Emails: $("#bcc_Emails").val()
};
$.ajax({
url: "/sendMailsToArtists",
type: "POST",
cache: false,
async: true,
data: data,
//data: $("commentform").serialize(),
success: function(data){
alert('Success!')
console.log("This is executingggggg");
}
,
error: function(jqXHR, textStatus, err){
alert('text status '+textStatus+', err '+err)
}
});
//return false;
}
<form class="form-horizontal" name="commentform" id="commentform">
//Some fields
<button type="submit" value="Submit" class="btn btn-custom pull-right" id="send_btn" onclick="sendMails()">Send</button>
</form>
The URL is contained in a file called app.js which I run for the server.
The line "This is executing" gets printed on the console. And then the alert pops up with the message "text status error, err". And then the page navigates to a URL which is something like "http://localhost:3000/contactArtists?first_name=ohohoo&email=mallika13055%4…"
Excuse me if it's something really basic.
EDIT:
Okay so I changed to <button type = "button">.
Here's the server side code:
app.post('/sendMailsToArtists', function (req, res){
console.log(req.body);
});
The data gets printed on the terminal. Does this mean there's no error? Then why doesn't the success branch of the ajax request get executed?
EDIT 2:
It's resolved. res.send() does the job.
jqXHR.status,jqXHR.statusText, andjqXHR.responseXMLto console.log.