DCHP
DCHP

Reputation: 1131

Get url status using jquery

I am trying to get the status of a 403 forbidden error, i can do it with php curl but need to do it using jquery.

Here is my code.

   $.ajax({

    url: 'https://s3.amazonaws.com/isdukg/01-jaimeson-complete_2_step_mix-pulse.mp3',
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    statusCode: {
      403: function() {
      alert("Forbidden");
      // or do something useful
      }
    }
});

$.ajax({

    url: 'https://s3-eu-west-1.amazonaws.com/fkcreate/Angles%2C%20Circles%20and%20Triangles..%20Instrumental.mp3',
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    statusCode: {
      403: function() {
      alert("Forbidden");
      // or do something useful
      }
    }
});

One of the urls will produce a 403 forbidden error which you can see here if you open the console window and load the page - http://scaredem.users36.interdns.co.uk/

I cant seem to grab that error to do something useful with it.

Upvotes: 1

Views: 1783

Answers (2)

tusar
tusar

Reputation: 3424

May be a timeout help you in this situation. If the request gets timed out in 500 milliseconds then no response (I tried to get the response status also, but no luck).

jsfiddle

            $.ajax({
                url: "https://s3-eu-west-1.amazonaws.com/fkcreate/Angles%2C%20Circles%20and%20Triangles..%20Instrumental.mp3",
                type: "get",
                dataType: 'jsonp',
                crossDomain : true,
                timeout : 500, // it is milliseconds
                complete : function(xhr, responseText, thrownError) {
                    if(xhr.status == "200") {
                       alert(responseText); // yes response came
                    }
                    else{
                       alert("No response"); // no response came
                    }
                }
           });​

Upvotes: 1

supertopi
supertopi

Reputation: 3488

Using jQuery ajax, you can map functions to HTTP response status codes using the statusCode property.

$.ajax({
statusCode: {
  403: function() {
  alert("Forbidden");
  // or do something useful
  }
}
});

Upvotes: 4

Related Questions