Location header returns NULLs and an entire object

$.ajax({
	url: "item.php",
	type: "GET",
	success: function(data, status, xhr) {
		console.log(xhr.getResponseHeader("Location"));
	}
});

I have this snippet above, but instead of returning it’s location. It returns about 15 NULL’s and then entire object and then one more null. This isn’t a loop, it’s a single file never called again. I’m trying to get location that it’s landed on (it’s being redirected there by server-side script), and I need to know the location it finally lands on.

Folk mentioned statusCode but that only returns int(3) and returns 200 when redirected as well. It has nothing for me.

then you should try the statusCode callbacks, not the statusCode() method.

I don’t see how anything like this will help:

$.ajax({
	statusCode: {
		404: function() {
			alert( "page not found" );
		}
		/* neither will 301, 307 or 308 */
	}
});

And neither statusCode nor statusCode() actually exist. As a variable or function:
Uncaught ReferenceError: statusCode is not defined

When I mentioned (and you quoted):

Folk mentioned statusCode but that only returns int(3) and returns 200 when redirected as well. It has nothing for me.

statusCode callbacks do nothing for me because:

I’m trying to get location that it’s landed on (it’s being redirected there by server-side script), and I need to know the location it finally lands on.

With which statusCode won’t help me because

Folk mentioned statusCode but that only returns int(3) and returns 200 when redirected as well. It has nothing for me.

I can’t get resulting location from statusCode. Because it doesn’t return 301, 307 or 308, when it’s redirected, it has no use for me. HTTP response code doesn’t help because it always returns 200. This snippet:

$.ajax({
	url: "item.php",
	type: "GET",
	statusCode: {
		200: function() {
			console.log("It worked");
		},
		301: function() {
			console.log("It didn't work");
		}
	}
});

Always returns “It worked” whether redirected or not.

Like I already mentioned:

Folk mentioned statusCode but that only returns int(3) and returns 200 when redirected as well. It has nothing for me.

the redirect status code is 302 …

Once again.

$.ajax({
	url: "item.php",
	type: "GET",
	statusCode: {
		200: function() {
			console.log("It worked");
		},
		301: function() {
			console.log("It didn't work");
		},
		302: function() {
			console.log("302");
		},
		303: function() {
			console.log("303");
		},
		304: function() {
			console.log("304");
		},
		305: function() {
			console.log("305");
		},
		999: function() {
			console.log("999");
		},
		666: function() {
			console.log("Any number you want");
		},
		69: function() {
			console.log("69");
		},
		25: function() {
			console.log("25");
		},
		"comeonman": function() {
			console.log(911);
		}
	}
});

Will return 200 “It worked”. Because the page that it lands on returns 200. It does not pass 301, 666, 69, 303, 302, 304, 209 or anything else. It passes headers from last file it gets. It doesn’t get triggered by any files it goes through. It does not help me get location of page script has landed on. Status code will tell only if script was successful, but won’t tell me the URL. The content of last file are shown, as expected (server redirects user before showing any content). But I need last URL visited. I need to “get URL where this data comes from”.

That can be done by using closure, to have the success function retain knowledge of the url that you sent it to.

function itemSuccessHandler(url) {
    return function itemSuccess(data, status, xhr) {
        // retains knowledge of the url
        console.log(url);
    };
}
$.ajax({
    url: "item.php",
    type: "GET",
    success: itemSuccessHandler("item.php");
});

When the success property is assigned to ajax, the same function that you had before is assigned to it but now with the additional knowledge of the url too.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.