I have this ajax call in my js. With this request I want to know if there is a previous opened session, in that case it will show the login form.
So, if there is no previous session, the server returns error 400 bad request (laravel response class). But when the server returns, the $.ajax object does not trigger the error function. Anyone knows why?
Server response
$data =[
'error' => [
'message' => 'Authentication Error',
'statusCode' => 400
]
]
Response::json($data, 400, [])
->setCallback(Input::get('callback'));
Ajax request
$.ajax({
global: true,
method: 'POST',
dataType: 'jsonp',
url: 'http://localhost:8082/api/checkAuth?callback=?',
success: function(response) {
return console.log(response);
},
error: function(a, b, c) {
console.log(a);
console.log(b);
return console.log(c);
}
});
I tried too getting the status code to trigger a function, but it does not work anyway:
** Ajax Request **
$.ajax({
global: true,
method: 'POST',
dataType: 'jsonp',
url: 'http://localhost:8082/api/checkAuth?callback=?',
success: function(response) {
return console.log(response);
},
statusCode:{
400: function(a,b,c){
console.log(a);
console.log(b);
console.log(c);
}
}
});
Thank you guys, for the help
SOME UPDATES:
Thanks to manish, I discovered this plugin for handling jsonp request, that triggers the error function. Does anyone know a way to capture the returned status code in the error function?
I assume that the server is returning 400 because it expects a POST request, jsonp is only capable of sending GET requests. Instead of using jsonp, you might want to use CORS.