How do I make a Restangular POST? - angularjs

I have the following in my Angular Model:
return Restangular.all('user/session/authenticate').post({user_id: user_id, password: password});
Which I call in my controller:
User.authenticate($scope.user.userId, $scope.user.username)
.then(function (authToken) {
//do stuff
};
But, it never makes a request. The headers are created, the data is there, and there is no error. I know it's probably me, but for the life of me, I can't figure out what. Also, I can successfully make GET requests to the same service.

Well, the problem wasn't me...sort of. I had the correct Restangular syntax, but a lack of understanding of how my browser works. The browser wanted me to confirm a security exception. No error was given, until I put the url directly into the url bar in FireFox.
Once I confirmed the security exception, the requests began to work.

Related

display error message soap request in Angular.js

I'm consuming a web service via SOAP. I work on my project request and get a response expected result. the problem that I have is because I want to know when the request fails.
I want that when the request is not complete either by internet problems, timeout, etc., to be displayed a warning (console.log) stating that there is no internet or "try again." I have tried many things, but only the error is displayed by default console google chrome reporting that no internet or whatever.
the console.log() is never shown the error message console appears only google chrome console.
Not to put all my code, so basically I generate a SOAP request:
$soap.post(url,action,params).then(function(response){
//my code..
});
//.success or .error not works ..
//i need for example:
/*
.error(function(response,status,headers,config) {
console.log("problems with your connection")
});*/
documentation of soap in angular:
https://github.com/andrewmcgivery/angular-soap
sorry for my level of english.
According to the documentation (and the code), the post call returns a promise. That means you should be able to do:
$soap.post(url, action, params).then(
function(response) {
// your code
},
function(reason) {
console.log("Problems with your connection");
}
);
Promise documentation: https://docs.angularjs.org/api/ng/service/$q

ng-token-auth, ionic, devise_token_auth; token get lost randomly in xhr request

I have inherited a Ionic app which uses ng-token-auth+devise_token_auth to handle the authentication and the session between front and back.
What happens is quite strange. Sometimes (specially with slow connections) the request (or the response) get lost and after that I get only 401 http errors.
I know that that everytime I send a request the token expires, but when the xhr request is cancelled (by the server I suppose, or by the browser, I don't know) the token is expired without having been replaced by the new one generated by devise_token_auth gem.
I know Rails but I'm not familiar with Angular, neither Ionic and I don't know exactly where to look.
After reading a lot of SO answers where noone seems having my problem (which happens locally and in staging/production), I checked the following
storage is set as localStorage.
config.batch_request_buffer_throttle = 20.seconds
there is no pattern between cancelled requests, sometimes I perform get for the username, sometimes a post or a put to a comment.
Is not a CORS problem because it would happen always or never. (moreover I'm using a proxy as explained in ionic blog)
Maybe it could be related to provisional headers chrome bug. But, how can I be sure?
What puzzles my is that it happens only sometimes and not always. (and there are no errors in the backend)
The only workaround I have found in the devise_token_auth documentation is change config.change_headers_on_each_request to false avoiding in this way the regeneration of the token.
But I don't like this solution because I think it hides the real problem in an insecure way instead of solving the token loss. Any suggestion?
Kindly, please check this thing:
Version: which version of this gem (and ng-token-auth, jToker or Angular2-Token if applicable) are you using?
Request and response headers: these can be found in the "Network" tab of your browser's web inspector.
Rails Stacktrace: this can be found in the log/development.log of your API.
Environmental Info: How is your application different from the reference implementation?
This may include (but is not limited to) the following details:
Routes: are you using some crazy namespace, scope, or constraint?
Gems: are you using MongoDB, Grape, RailsApi, ActiveAdmin, etc.?
Custom Overrides: what have you done in terms of [custom controller overrides]
5?
Custom Frontend: are you using ng-token-auth, jToker, Angular2-
Token, or something else?

How to debug a script put in module's config?

My AngularJS project is working with an API. This API provides authentication tokens (Oauth): an access_token and a refresh_token.
Everytime an AngularJS request to the API returns a 401 error, it means that the access_token has expired and it needs to be refreshed by sending the refresh_token to a specific URL. To do that, I followed this tutorial.
But this is not working and I don't know why. I would like to debug the function placed into .config(...) but I don't know how to do that. console.log() and $rootScope = ... doesn't work here.
Thanks for your help !
Use your browser's built-in debugger.
Chrome: https://developer.chrome.com/devtools/docs/javascript-debugging
Firefox: https://developer.mozilla.org/en-US/docs/Tools/Debugger
Firebug: http://getfirebug.com/javascript
IE: https://msdn.microsoft.com/en-us/library/gg699336%28v=vs.85%29.aspx

AngularJS "caching" on slow internet connection :(

I've a simple angularjs application like:
module = angular.module 'testFooBarModule', ['ngResource']
module.factory 'FooBar', ($resource)->
$resource 'foobar/target', {},
load:
method: 'GET'
cache: false # no effect
headers:
'Cache-Control': 'no-cache' # no effect
module.controller 'FooBarCtrl', ($scope, FooBar)->
FooBar.load (result)->
console.log result
With highspeed internet like home wlan or mobile hspa everything works fine. The request has fired and the server gives me a unique response.
But with 2G internet like edge or gprs the server receives no requests and the angularjs app give me always the same results. No errors occur. The "no-cache" header were sended as well.
I've no idea what's wrong. Can anyone help me?
Sry for my english :D
best regards.
Thats probably not an angular issue. My guess is the internet-provider is doing some magic http-proxying to safe bandwidth or your underlying OS is trying to cache it for you.
Some options:
using HTTPS should prevent provider man-in-the-middles and for mobile apps in any case a good idea!
using a clumsy dynamic parameter like a timestamp
use POST or PUT as verbs
add the 'Cache-Control: no-cache' header in the server-response, which I think the is the most-clean solution

Angular $resource.save() not working?

I am trying to create a web app with Angular, and I need some help. I need to POST data to an api. Rather than using $http (which works), I'd like to use $resource, and save().
I've also used GET and query so far, and they work fine. But when I try to call save(), I never get a response. I also don't see a POST request go through when I check on the server side.
Here's my code for the $resource factory (normally with my actual API url, of course)
.factory('ExamplePosts', function($resource) {
return {
all: $resource('http://my.website.com/api/')
};
});
In my controller, I can run ExamplePosts.all.query() or .get(), and retrieve the results. (I also see the request in my server logs.)
I then tried running ExamplePosts.all.save(), and I did not see a POST request on the server logs. I can post to it through forms and it works, but there seems to be a problem with Angular.
Appreciate any help I can get, thanks!
You actually need to add a prefix "$" to the save method as .$save() not .save() read this for more on those that need the $prefix read more

Resources