AngularJS Http Request return 401 from a SailsJS server with Electron - angularjs

I'm building a Electron App. Any request I make with Angular running on the Electron App return with unauthorized.
I already checked for the poilicies from Sails, disabled all policies. If I make a request for the same url from Postman or even Chrome it works ok.
$http.get(CONFIG.API + 'auth/login', $scope.login).then(function (data) {
console.log(data);
});

It's 100% config/cors.js issue. Make sure you have uncommented this line of code.
allRoutes: true,

Related

Axios Post call getting 502 proxy error in production where as request is server is happening and success response

Can anyone help, as I couldn't recreate this issue to check why this is happening.
I was using axios for calling rest api in my react application, I couldn't find any issue while developing and in uat , but when the code went live , in production always axios one post call is getting 502 proxy error but the same request was made to rest service and in backend everything is success. So , backend is success but user is seeing unsuccess page as it is getting 502 error.
Everything network and load balancers couldn't find any issue.
Example Code
this.postapi().then(function (response) {
//doing something
});
postapi() {
return axios.post('/postapi', {
// request data
}
}).catch(function (error) {
console.error(error);
if(error.response.status === 500 ){
//error page redirect
}else{
//unsuccess page redirect
}
});
}
from above code, always the API call is being done and success in rest service, but getting 502 proxy error and going to catch block and showing unsuccess page
This behavior is only happening in production
Environment:
Axios Version [e.g. 0.19.0]
Browser [e.g. Chrome, Safari]
React 16.9.0

Ionic 3 HTTP Status Code 401 changed to 0

I am using ionic 3 with spring boot at the back end. Spring Boot API are secured with Spring Security JWT Token.
While tying to execute any API, if the token has not expired the API is working properly.
Once token is expired its expected that API should return 401 status code, but in the ionic its always returning status code a 0(Zero).
However if we check the Network in the chrome browser, its showing correct response code as 401, but the ionic code its coming as 0(Zero)
Ionic Code Sample
var url = this.masterConfig.ADMIN_SERVER_URL +'/home';
this.http.get(url,getOptions).subscribe(data => {
console.log('Data :::'+data);
},error =>{
console.log('Error :::'+error);
console.log('Error :::'+error.status);
console.log('Error :::'+error.error);
});
Network Logs
Console Log
If you happen to be using the Auth0 Angular2-jwt component, then this behavior has been reported as an issue. That's the only time I've personally run into this behavior with Ionic 3 and HTTP response issues.

Angular $http.post not working, works using postman

I've created an Ionic app which calls an API to post the user's current location.
The request works as follows:
POST: http://mywebsite.com/api/Patients/AddLocation/17
with body:
{
"Latitude": 51.3753786,
"Longitude": -0.0833691
}
However, the following code in my Ionic app does work:
$http.post('http://mywebsite.com/api/Patients/AddLocation/' + $scope.id, data)
.success(function () {
console.log('Updated location');
})
.error(function (error) {
console.log('Error updating location');
console.log("Error: " + error);
});
In which 'data' is the same as the body above.
Any ideas why this isn't working?
UPDATE:
Here's a couple of screenshots of the network request:
Network request available at imgur RWNDF.png
Postman request
It happens if you have not enabled cors in your server.
enable cors in you server.
even if you dont enable cors,
get method will work peerfectly if you have enabled cors using any extension in chrome.
It's because of CORS. Enable cros from the server end and the a header will be set in HTTP Access-Control-Allow-Origin: * .
If your server app is an expressjs app, use below code to enable CORS
var cors = require('cors');
.....
app.use(cors());
Else use chrome extension Allow Cross Origin Request
This should solve the problem
After comparing this to a sister app which uses the same API and works, the only difference I could see was that a few of the plugins were different.
To remedy this I deleted the plugins folder and the platforms folder, re-added android and just the plugins I need, it now works. I guess it's maybe some bug I created by using the wrong mixture of plugins.
A very unsatisfying answer, but all requests work now!

Waterlock, Sails, Angular JWT token gives 403 from browser

backend: Sails app w/ Waterlock, Waterlock-local-auth
frontend: An angular app
I'm running into a issue where postman works fine, gets JWT from /user/jwt all good and dandy but whenever I login through the browser and then attempt to hit /user/jwt it gives 403 forbidden. Any clues?
I have CORs enabled for all origins on all routes.
Any help would be appriciated!
To save some time for novices like me:
$http won't be passing the session data with the request, so Sails/express server doesn't know which 'session' the /user/jwt request is being made from and hence giving 403, as it should. I guess Postman handles this for you automagically.
Added this to my angular config:
config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.withCredentials = true;
}]).

Phonegap angularjs $http.post() always returns http code 200

Angularjs v. 1.2.0rc3 with phonegap 3.1 and django-rest-framework
When posting data to my API (login form) I am getting status 200 in angularjs no matter if I authenticate with proper credentials etc. Even though I simply return HTTP_400 in the API view as in the example below, angularjs thinks this is a HTTP_200 status code.
I am sure the backend API returns HTTP_400 which works fine when using local http server.
I can also confirm this by debugging the api view.
Here's the example code.
Django Rest Framework code:
class LoginView(APIView):
def post(self, request):
return Response({}, status=status.HTTP_400_BAD_REQUEST)
and my angularjs controller:
$scope.login = function () {
$http.post(API_URL, $scope.loginUser).then(
function(response) {
console.log('STATUS : ' + response.status);
console.log('request OK');
},
function() {
console.log('request is NOT OK');
)};
As far as I know phonegap uses a local file:// protocol so there should be no CORS
related problem ?
When the same on chrome browser, I get Origin file:// is not allowed by Access-Control-Allow-Origin. Adding --disable-web-security flag to chrome startup script eliminates this problem but it does not solve the "always 200 status code" for phonegap.
Testing on android 2.3 & phonegap 3.1 & angularjs 1.2.0rc3
Confirmed fixed in AngularJS 1.2.3
See the line here:
https://github.com/angular/angular.js/blob/v1.2.3/src/ng/httpBackend.js#L122
Upgrade to Latest(version 1.2.9) AngularJS version, Its resolved now.

Resources