0

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?

  • Not tried `js` at server-side. Please illuminate. Thanks. – guest271314 Apr 12 '14 at 23:44
  • You haven't used Node yet? - *pulls man from dark cave* - Here: http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js , I give you ... ***Fire***. ----- @guest271314 –  Apr 13 '14 at 00:07
  • Thanks ! Found this http://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js . Part issue with `$.ajax()` pieces may be that `data:type` at `ajax()` set to `json`, yet may not be `json` returned ? Only `header`, or `textStatus`, not `json` ? Also, `textStatus` not `parameter` within `success(data, textStatus, jqxhr)`, `error(data, textStatus, jqxhr)` callbacks ? Is `res.writeHead` `json` format ? – guest271314 Apr 13 '14 at 01:10
  • Your code is far clearer than your English. Maybe if you were to speak to me in pure code, we would understand each other better. With that concoction there though, I'll admit I'm quite confused. @guest271314 –  Apr 13 '14 at 01:13
  • Ok in minute, two. Thanks ! – guest271314 Apr 13 '14 at 01:17
  • @guest271314 I do appreciate your effort though. thank you. –  Apr 13 '14 at 01:19

1 Answers1

0

Note Not tried node.js, node.js pieces not addressed

Edit

See at original post

server-side (response)

 'Content-Type': 'text/plain'

js (request)

dataType: "json"

possibly trigger error handler ?

Note

At server-side response, Content-Type appear set to text/plain , $.ajax() settings for expect json (application/json) response ? Perhaps why error callback called each instance ? If could a) set expected response to text/plain, b) chain callback within error() or fail() callback, which get called when content-type not json returned, c) change server-sideContent-Type setting (i.e.g., to json), generate and substitute application/json response for text/plain ?

Try this (pattern)

html

<p id="results"></p>
<button>localpost</button>

js (updated)

$(function() {

var localpost = function() {    

var callback = function(name) {
                 $("#results").html(name)
               }; 

var request = $.ajax({
    url : "/echo/json/",
    type:"POST",
    dataType: "json",
    data: { json : [] },
    statusCode : {
    200: function (data, textStatus, jqxhr) {
            if (textStatus === "success" 
               && jqxhr.getResponseHeader("Content-Length") === (0 || null)) {
               alert(textStatus
               +"\nCould not connect to the server! Returning to login screen.");

               callback("textStatus: "
               + textStatus+"<br />jqxhr status: " +j qxhr.state()
               + "<br />content-length : " 
               + jqxhr.getResponseHeader("Content-Length")
               + "<br />status : "+jqxhr.status);
       // signOut();
            };

    }
  }
});
        // `success` callback
request.then(function(data, textStatus, jqxhr) {
    if (textStatus === "success") {
        // DoStuff()
       console.log(textStatus);
    };  
},
        // `error` callback
  function(jqxhr, textStatus, errorThrown) {
    if (textStatus != "success") {
        // DoStuff()
       console.log(errorThrown);
    };  
})
};

    $("button").on("click", localpost);
    // localpost()    

});

jsfiddle http://jsfiddle.net/guest271314/Cer4G/

guest271314
  • 1
  • 15
  • 104
  • 177