Thursday, 18 May 2017

Simple Json Request never succeeds

I have a very simple Express server with a single route. I want to call this route with ajax from my html page and return json to the page.

The server side function gets called successfully but the failure method of the ajax method gets called the whole time. It never succeeds.

NodeJs

app.get('/test', function(req, res) {
  console.log("TEST WAS CALLED");// This Logs to Console
  res.json({ message: 'Hello World' });
});

Client Ajax

function FetchData(callbackfn) {
        $.ajax({
            type: "GET",
            url: "http://localhost:3000/test",
            async:false,
            cache:false,
            success: function (data) {
                callbackfn(data)
            },
            error: function (textStatus, errorThrown) {
              console.log(errorThrown);
              console.log(textStatus);
                callbackfn("Error getting the data")
            }

        });
     }
        function Callback(data)
             {
                alert(data);
             }



via dubbeat

No comments:

Post a Comment