Here is a server-side function that receives a POST request and handles its response. As you can see, when no error occurs, the status code of the response is 200.
app.post('/submit', function(req, res) {
var p = new Promise(function(resolve, reject) {
db.serialize(function() {
db.run("INSERT INTO users VALUES (?, ?, ?, ?)",
[req.query['email'], req.query['company'], req.query['subject'], req.query['text']],
function (err) {
if (err) {
console.error(err);
reject();
} else {
console.log("Transaction passed");
resolve();
}
});
});
});
p.then(function(){
res.status(200).end();
}).catch(function() {
res.status(400).end();
})
});
In the client side, I wrote a JS function that sends an AJAX POST request to the server and handles the response:
function addFormToDB(email, company, subject, text) {
var xhttp = new XMLHttpRequest();
var params = "email=" + email + "&company=" + company + "&subject=" + subject + "&text=" + text;
xhttp.open("POST", "http://127.0.0.1:3000/submit?" + params, true);
xhttp.onreadystatechange = function() {
console.log(xhttp.readyState + " " + xhttp.status);
if (xhttp.readyState == 4 && xhttp.status == 200) {
console.log("request " + params + " was sent to DB");
alert("Thank You!");
}
};
xhttp.send();
}
When I invoke the function 'addFormToDB' from the console window of the browser, it sends an AJAX request to the server and gets it back with an xhhr.status of 200.
However, 'addFormToDB' has to be invoked when the user fills a form and clicks the 'submit' button. When this happens, the xhhr.status sent back from the server to the browser is always 0. I don't know why.
Here is the HTML code that creates the form:
<form id="form">
<label for="email"> E-mail: </label>
<input id = "email"> <br>
<label for="company"> Company: </label>
<input id = "company"> <br>
<label for="subject"> Subject: </label>
<input id = "subject"> <br>
<label for="text"> Text: </label>
<input id = "text"> <br>
<input type="submit" value="Submit" id="button"><br><br>
</form>
...
<script>
$("#form").submit(function() {
addFormToDB($("#email").val(), $("#company").val(), $("#subject").val(), $("#text").val())
});
</script>
Can you help me find out the bug?