I have an ajax call that is partly used to check to see if you are logged in.
function userInfo(){
return $.ajax({
url: "http://api.itassistteam.com/api/account/isloggedon",
type: "GET",
dataType: "json",
xhrFields: {
withCredentials: true
},
statusCode: {
401: function(){
console.log("401 error.");
deleteCookie();
window.location.href = "./login.html";
}
},
});
}
In Chrome, FF and Opera, it redirects to the log in page like it's supposed to, but in IE it does nothing. When I look in the console, it says
SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.
Any idea why IE will not take the error handler and redirect to login? The "401 error." message does not appear in the console.
Instead of statusCode I used error. This is how you get the same results with error that you would get with
statusCode : {
401: function(){...}
}
You do,
error: function(jqXHR, textStatus, errorThrown){
if(jqXHR.status == 401){
...
}
}
This is consistent across all major browsers unlike the statusCode method witch doesn't work with IE. This way you can have error handling specific to the error in IE.
Related
I'm using jquery to make an AJAX call that returns an HTTP 500 error. Works fine in firefox, but IE reports a 200 error. I know the PHP is right, because if I go to the page directly, it shows up as a 500 error. It's just the jQuery part that doesn't work in IE.
Here's the ajax call:
$.ajax({
url: "download.php",
data: {
removefile: filename
},
type: "GET",
dataType: "html",
success: function(html) {
alert("Success");
},
error: function(xhr, status, errorThrown) {
alert("Error: " + xhr.status);
},
complete: function(xhr, status) {
alert("Complete: " + xhr.status);
}
});
The php I'm using is something like:
header($_SERVER['SERVER_PROTOCOL'] . " 500 Internal Server Error", true, 500);
echo "Remove failed";
I'm testing on IE 9, but tried all of the different compatibility modes with the same issue.
Any ideas?
Duh! Changing the request type from "GET" to "POST" solves the problem. Guessing IE was caching the response. Not the same question, but Error with Internet Explorer and Jquery Ajax has a similar answer.
Sorry for the noise.
I am performing one cross domain get operation as shown below.
$.ajax({
type: "GET",
url: "http://localhost:65249/api/item/get",
data: {
searchText: "test"
},
dataType: "jsonp",
async: false,
success: function (results) {
alert(results);
},
error: function (jqXHR, error, errorThrown) {
if (jqXHR.status && jqXHR.status == 401) {
alert("Unauthorized request");
} else if (jqXHR.status && jqXHR.status == 404) {
alert("The requested page not found");
}
}
});
But success or error block is not getting called after request is completed. when i debug java script in developer console i am receiving error but error block of javascript is not getting called.
GET http://localhost:65249/api/item/getallproducts?callback=jQuery182028460139059461653_1396510235829&searchText=test&_=1396510674779 401 (Unauthorized)
Unfortunately, if you are using JSONP, all requests that return an error fail silently. This is because JSONP uses a script tag instead of XmlHttpRequest. If you want errors to fire, you need to use XHR with CORS. CORS needs to be configured on the server side, and it works client side only in IE 10+.
error dont work on corss domain calls, see jquery doku. for error:
Note: This handler is not called for cross-domain script and
cross-domain JSONP requests.
Take a look at this answer:
JSONP request error handling
I have the following javascript code that performs a POST to an API for a login page:
do_post_form_data = function (relative_url, access_token, postData) {
return $.Deferred(function (def) {
$.ajax({
type: 'POST',
url: baseURL + relative_url,
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
data: postData
})
.done(function (data) {
def.resolve(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
var error = jqXHR.responseJSON ? jqXHR.responseJSON : { error: "", error_description: "POST failed." };
def.reject(error);
});
});
},
If the login fails (invalid credentials are sent), the API returns a 401 Unauthorized error to the caller with a JSON object in the body explaining why the login failed. The code works as expected when sending the data from the latest versions of Chrome, Firefox and Safari. The responseJSON object exists on the jqXHR object, and I can retrieve the error messages.
However, IE10 seems to be intercepting the 401 error and trying to redirect to a login page, for which the API server does not provide a URL, though I cannot see any additional calls being made to the server in Fiddler. My AJAX call never sees the 401 error but winds up receiving a 404 error with no JSON data in the jqXHR object.
Has anyone seen this behavior before and found a way to make IE report the initial 401 error to the AJAX call? IE10 with jQuery 2.0.2.
Thanks for any suggestions.
I'm having a lot of trouble with the 307 Temporary Redirect statusCode using jquery.
I have an apache server that returns a 307 when needed. The 307 is getting thrown properly (I can see this with firebug or a wireshark trace), but I can't seem to get the redirect to trigger.
$.ajax({
url: someURL,
type: 'GET',
processData: false,
data: someData,
cache: true,
timeout: 5000,
statusCode: {
307: function() {
alert('307'); // this does NOT get called
}
},
error: function(request, status, error){
try {
console.log(request.getAllResponseHeaders());
}
catch (err) { }
},
success: function(data, textStatus, response){
try {
console.log(response.getAllResponseHeaders());
}
catch (err) { }
});
I wanted to check out the headers so I could redirect using the getAllResponseHeaders() method. This works fine for 200's but the 307 never triggers (in the success nor error handler)
One thing I noticed when using Firebug to examine the traffic is that right after the redirect an ajax GET is sent to the server with the url of the redirect! This results in a failure however.
So, what gives?
Edit:
I tried this with jquery 1.5.1 and 1.6.1
Well, I did some more research and it appears that a 307 is automatically handled by the browser, and is not able to be intercepted by jquery.
I ended up using a different statusCode to do the redirect (410 Gone).
This probably isn't the best solution, but I don't have access to the apache server code.
I'm building a small Chrome extension that must send messages through a POST http request to a server in my company network, and I'm using jQuery 1.4.1 to speed up the development of the javascript part.
I have this code to send the request:
function send() {
$.ajax({
url: "http://mycompany.com/update",
method: "POST",
data: {status: "sometest", in_reply_to_status_id: "anId"},
success: function(data, textStatus) {
console.log("success");
console.log(data);
console.log(textStatus);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("error");
console.log(XMLHttpRequest);
console.log(textStatus);
console.log(errorThrown);
},
complete: function(XMLHttpRequest, textStatus) {
console.log("complete");
}
});
}
The request done this way fails, in the Chrome log I see that the server responds with a http status 400 and with the text "This methods requires POST".
If I change to code above with this:
function send() {
$.post("http://sunshine.emerasoft.com/statusnet/api/statuses/update.xml", {status: "sometext", in_reply_to_status_id: "anId"}, function(data) {
console.log(data)
});
}
everything works fine, the http status is 200 and server side I can see that the data I sent is correctly saved.
I need to use the full $.ajax() method because I need to do some work in case of success or failure, and some other when the request is complete, so $.post() is not enough.
Am I doing something wrong calling $.ajax(), or there is an issue of some kind, maybe because I am in the xontext of a Chrome extension?
Thanks
I believe the $.ajax() function takes a 'type' option, not a 'method' option.
The default type is GET.