In the following code, all I am trying to do is to get the HTTP response code from a jQuery.ajax call. Then, if the code is 301 (Moved Permanently), display the 'Location' response header:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>jQuery 301 Trial</title>
<script src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
<script type="text/javascript">
function get_resp_status(url) {
$.ajax({
url: url,
complete: function (jqxhr, txt_status) {
console.log ("Complete: [ " + txt_status + " ] " + jqxhr);
// if (response code is 301) {
console.log ("Location: " + jqxhr.getResponseHeader("Location"));
// }
}
});
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$('a').mouseenter(
function () {
get_resp_status(this.href);
},
function () {
}
);
});
</script>
</head>
<body>
<a href="http://ow.ly/4etPl">Test 301 redirect</a>
<a href="http://cnn.com/not_found">Test 404 not found</a>
</body>
</html>
Can someone point out where I am going wrong? When I check the 'jqxhr' object in Firebug, I can't find the status code, nor the 'Location' response header. I set the breakpoint on last line of 'complete'.
Thanks much.
It is probably more idiomatic jQuery to use the statusCode property of the parameter object passed to the the $.ajax function:
However, as Livingston Samuel said, it is not possible to catch 301 status codes in javascript.
jqxhr is a json object:
see: jQuery ajax
so you would do:
jqxhr.status
to get the statusWhen your XHR request returns a Redirect response (HTTP Status 301, 302, 303, 307), the
XMLHttpRequest
automatically follows the redirected URL and returns the status code of that URL.You can get the non-redirecting status codes (200, 400, 500 etc) via the
status
property of the xhr object.So you cannot get the redirected location from the response header of a
301
,302
,303
or307
request.You might have to change your server logic to respond in a way that you can handle the redirect, rather than letting the browser do it. An example implementation.
I see the status field on the jqXhr object, here is a fiddle with it working:
http://jsfiddle.net/magicaj/55HQq/3/
You can check your respone content, just console.log it and you will see whitch property have a status code. If you do not understand jsons, please refer to the video: https://www.youtube.com/watch?v=Bv_5Zv5c-Ts
It explains very basic knowledge that let you feel more comfortable with javascript.
You can do it with shorter version of ajax request, please see code above:
Example console output:
Came across this old thread searching for a similar solution myself and found the accepted answer to be using
.complete()
method ofjquery ajax
. I quote the notice on jquery website here:To know the
status code
of a ajax response, one can use the following code:Works similarily for
.done()
and.fail()