Using AJAX to check for server connectivity
I'm trying to use a simple client AJAX post to check that the server is up before proceeding to functionality. I don't want to send any data, so I've attempted to respond to the client with a success header and leave out the body, but this is causing the client to trigger the error handler, even when the server responds.
Here's the server-side snippet that handles posts to "check" from within my listener:
app.post('/check', function (req, res) {
console.log("Client checking for service...");
res.writeHead(200, {
'Access-Control-Allow-Origin': 'http://localhost',
'Content-Length': 0,
'Content-Type': 'text/plain'
});
res.end();
console.log("Client checked in!");
});
Here's the AJAX request:
request = $.ajax({
url: "http://127.0.0.1:8080/check",
type: "POST",
crossDomain: true,
data: '',
dataType: "json",
error: function () {
alert("Could not connect to the server! Returning to login screen.");
signOut();
},
success: function () {
DoStuff();
}
});
Does sending a successful response header without a response body cause an error in an AJAX request?