0

I'm writing a small code trying to get a return of the server http status

Currently I have worked out the following code (which works), but I am wondering why the call is skipping the success and going straight to error.

This is the code I currently have:

$(document).ready(function(){
        $("#test-button").on("click", function() {
            $.ajax({
                type: "GET",
                dataType: 'jsonp',
                url: "<<INSERT THE URL>>",
                success: function(response) {
                    alert(response.status);
                },
                error: function(response) {
                    if(response.status == 200) {
                        // Redirecting
                        window.location.href = '<<INSERT THE URL>>';
                    } else {
                        alert('The organisation you entered is not available');
                    }
                }
            });
        });
    });

Basically I just want to know why it's skipping the success, even when getting a 200 as return.

Thanks

Pex
  • 519
  • 2
  • 12
  • 30
  • can you remove the dataType: 'jsonp' and check ? – Alan Pallath Aug 10 '17 at 13:19
  • 1
    https://stackoverflow.com/q/6186770/20126 https://stackoverflow.com/questions/16230624/ajax-call-fires-error-event-but-returns-200-ok – Amr Elgarhy Aug 10 '17 at 13:20
  • Possible duplicate of [Ajax request returns 200 OK, but an error event is fired instead of success](https://stackoverflow.com/questions/6186770/ajax-request-returns-200-ok-but-an-error-event-is-fired-instead-of-success) – Amr Elgarhy Aug 10 '17 at 13:20
  • @AmrElgarhy Thanks, instead of using error/success, I simply use complete :) – Pex Aug 10 '17 at 13:32
  • @AlanPallath jsonp is used for crossbrowser; otherwise the response is a fail. – Pex Aug 10 '17 at 13:33

1 Answers1

0

This can happen if your response from the server is not valid jsonp. The documentation states:

Evaluates the response as JSON and returns a JavaScript object. (...) The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown.

MarkRobbo
  • 975
  • 1
  • 9
  • 31