AngularJs $http timeout response code - angularjs

If I have
$http.get(url)
.success(function(data, status, headers, config) {})
.error(function(data, status, headers, config) {});
what value would status have if ther were a timeout (no internet connection). Would it just be 404?

Sicne #HamletHakobyan won't post an answer, I will point out that his comment was correct - status is -1

Related

Give the more than one parameters in http request angularjs

My Laravel route is :
Route::get('/get_event_history/{data}/{date}', 'HomeController#GetEvents')->name('get_event_history');
and I wanted to pass two routes. I am doing like this:
$scope.HistoryEventClick=function(imei,date){
var Indata = {'data': imei, 'date': date };
$http.get('/get_event_history/',params: Indata).success(function (data, status, headers, config){
console.log("Event Data from the query",data.data);
}).error(function (data, status, headers, config) {
//alert(status)
});
}
Its not working and gives error route not found
You may need to change to this:
$http.get(`/get_event_history/${imei}/${date}`).......
$http.get('/get_event_history/'+$routeParams.imei +'/date/'+ $routeParams.date)

Retrieve $http error status

I use Angular $http GET request that respond with 401 status, as i verified with Chrome network tab.
Inside the errorCallback for this request the response.status is always -1. When i started to investigate it i found the source of -1 deeply inside angular file:
var requestError = function() {
// The response is always empty
// See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error
completeRequest(callback, -1, null, null, '');
};
So, how i can retrieve the status code?
Call it like this:
$http.get('/Url').
success(function(data, status, headers, config) {
}).
error(function(data, status, headers, config) {
});
Finally i found the reason is CROS error. when i changed API endpoint to responde with header: Access-Control-Allow-Origin:* the problem solved and i can see the status code.

"Must specify grant_type field" error when getting Oauth2 token

I've got a simple node.js backend build on Restify with OAuth2 authorization (restify-oauth2 plugin).
I've got a problem requesting a token. When I call POST method with my REST client, everything works correctly and I get access_token and token_type.
The problem occurs when I try to do the same thing in my frontend aplication made in Angular.
Here's my code:
var req = {
method: 'POST',
url: 'http://localhost:8080/token',
headers: headers,
data: {},
params: {grant_type: "client_credentials"}
};
$http(req).success(function(data, status, headers, config){
console.log('success');
}).error(function(data, status, headers, config){
console.log('error');
});
So as you can see, grant_type parameter is provided. But still the server responds with:
{"error":"invalid_request","error_description":"Must specify grant_type field."}
And here's devtools screenshot:
How do I need to change my request to make it work?
I finally solved it. The thing is that grant_type field must be passed inside a request body, not in the parameters.
Working request code:
var req = {
method: 'POST',
url: 'http://localhost:8080/token',
headers: headers,
data: "grant_type=client_credentials"
};
$http(req).success(function(data, status, headers, config){
console.log('success');
}).error(function(data, status, headers, config){
console.log('error');
});

Get Count of Rows via https request

i get the data for my table via $http request like this:
$http.post('/DesktopModules/MyServices/API/PService/LoadPObjectListData', request).
success(function (data, status, headers, config) {
....
My question is, how can i display the value of loaded rows while the request pending?
I think it's not possible , while the request pending. But you can do it whild the request succeed.
html
<span>{{DataCount}}</span>
and js side
$http.post('/DesktopModules/MyServices/API/PService/LoadPObjectListData', request)
.success(function (data, status, headers, config) {
$scope.DataCount=data.length
}

Cross Domain request returns status code 0 in Firefox but 200 in Chrome

I have code in a directive that is making a cross domain request. When it runs in Chrome, it works just fine. When it runs in Firefox or IE 11, I get a status code of 0 with no data. My code is:
$http({method: 'GET', url: 'https://mycrossdomain.url'})
.success(function(data, status, headers, config) {
alert('success');
})
.error(function(data, status, headers, config) {
alert('error');
})
In Firefox and IE I get "error" alerted and in Chrome I get "success" and I can view the data.
Why would this be happening? The server obviously supports cross domain requests because it works in Chrome. When I navigate to the URL in all browsers I get a response so it definitely exists. Any help would be appreciated.
Turns out the issue was that I needed to specify withCredentials like so:
$http.get('https://mycrossdomain.url', { withCredentials: true})
.success(function(data, status, headers, config) {
alert('success');
})
.error(function(data, status, headers, config) {
alert('error');
})
This is because I needed to pass the user credentials (a certificate) to the other domain. Angular was unfortunately not providing very useful error messages.

Resources