1

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.

2
  • opening the url directly in a browser produces a xml document with an error message. Commented Apr 20, 2012 at 10:50
  • The only thing I got was 'server does not support RFC 5746, see CVE-2009-3555'. Commented Apr 20, 2012 at 10:52

2 Answers 2

4

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
  }
}
});
Sign up to request clarification or add additional context in comments.

3 Comments

I to thought that this would be the correct way but it doesnt work i have updated the code
Maybe the response code is not 403, at least it wasnt when I tested your URLs. BTW, why is dataType declared jsonp if you are GETting an mp3-file?
I tried many different dataTypes to get it working from the documentation i could find a correct one for a audio file it their one?
1

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
                    }
                }
           });​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.