0

I have this ajax request, but its not doing what i wanted it to do, it was suposed to be a GET that returned some data but no matter how i set up the URL it always gives success response even though it was not a success,

 var id = 5;
$.ajax({
        type: 'GET',
        URL: "'../this is totaly wrong/" + id + "'",
        success: function(data) {

            console.log("success");
            window.location.replace("/formex");

 }
 })

The correct route should be "readForm/:id", but i write it like that and it give success anyway and redirects to the "/formex" page, the url i wrote here doesn't even exist it's just to prove that it gives success even though it shouldn't

  • Can you show how your server routes are setup? – Paul Jun 16 '18 at 12:03
  • What's the response that comes back from the server? Sometimes an "error" is returned with a success error code but as a page with a friendly error message. It all depends on what the server actually returns. – David Jun 16 '18 at 12:06
  • global.app.get('/readForm/:id', function(req, res) { var id = req.params.id; global.modelRespForm.readForm(function(err, data){ if (err) { // error handling code goes here console.log("ERROR : ", err); } else { res.send(id); res.end('{"success" : "Updated Successfully", "status" : 200}'); } }); }) – João Miguel Jun 16 '18 at 12:10

1 Answers1

1

Your server is doing a Console.log on error. Your server should set the HTTP Status to an error and return accordingly. In this case your Server is sending back a '200' which is the definition of a successful call. What you could do is:

  1. Return 200, but add to the returning body (maybe JSON) a field indicating an error. Then on your front end success handler read that field and decide if it's a valid request.

  2. Return error code, such as 500, and then on your front end catch an error and handle.

Please make sure you understand HTTP status codes.

Airwavezx
  • 898
  • 5
  • 14
  • 26