Cannot get the cookie in angular even using $cookies - javascript

I have seen all the questions regarding this topic in SO.However I cannot seem to get the cookie value even after implementing.I am a newbie at this.So please forgive me for the lack of understanding.
I am sending correct login credentials via angular ajax to server which is returning a Set-Cookie property having a value.I cannot get this cookie value even after using $cookies and implementing it within $timeout.
Below is my service code:
userOperation.loginService = function(username,password){
return $http({
method: 'POST',
withCredentials: true,
url: urlService.baseUrl+'login',
headers: {'Content-Type': 'application/x-www-form-urlencoded','Accept':'application/x-www-form-urlencoded'},
data:"username="+username+"&password="+password+"&AUTH_TYPE=RESTAUTH",
}).then(function successCallback(response){
console.log(response);
return response;
},function errorCallback(response){
console.log(response);
return response;
});
}
And the controller implementing it
app.controller('logInCtrl',function($scope,$http,$browser,$cookies,userOperation,pnotifyService,$rootScope,$timeout){
$scope.login = function(){
userOperation.loginService($scope.username,$scope.password).then(function(response){
console.log(response);
$timeout(function(){
console.log($cookies.get('JSESSIONID'));
console.log($cookies);
},4000);
});
}
});
The response is null object;
But using advanced rest client i am getting the cookie value.Below is the screenshot.
Thanks for your time.
Note:All angular cdns are of version 1.5.8.

Related

Angular http GET 405 (Method Not Allowed)

I'm implementing another function in an application (made with angular), I call an API (made with Lumen) and it returns a 405 error whem i tried in local and "XMLHttpRequest cannot load Response for preflight is invalid (redirect)" when i tried on the server. I read about CORS but it's already implemented, I think if it isn't none of others functions would work.
Another thing, if I make the call with Advanced Rest Client it works fine :(
sorry about my english, i don't speak it so much.
function on application
$scope.agencias = [];
$http({
method: 'GET',
//url: 'http://api-beta.grupoaldorf.com.mx/agencias/',
url: 'http://localhost:8000/agencias/',
headers: agenciaMazda.headers
}).then(function successCallback(response) {
$scope.agencias = response.data;
console.log("regreso del http");
console.log($scope.agencias);
}, function errorCallback(response) {
//callback(false);
});
route on web.php
$app->get('agencias','CitasController#agenciasDisponibles');
controller in CitasController.php
public function agenciasDisponibles(Agencia $agencia){
$fabricante = $this->request->header('Aldorf-App');
$disponibles = Agencia::where('fabricante', $fabricante)->where('mostrar', 1)->select(['codigo', 'nombre'])->get();
return $disponibles;
}
Finally i solved it, it was really dumb, i added a slash at the end of the uri xD it had to be "http://localhost:8000/agencias" instead of "http://localhost:8000/agencias/"

Angular $http(...).success is not a function help rewriting the function

I'm trying to fix a website for a group I volunteer for.
I'm trying to update it from Angular 1.3.16 to Angular 1.6.4, but I'm getting an error message that says:
TypeError: $http(...).success is not a function
at b.$scope.init (angular-custom.js:107)
The code that seems to be causing it from what I can tell by debugging it is the angular-custom.js file with the .success and .error functions:
$scope.init = function(){
$http({
method: 'post',
url: url,
data: $.param({ 'type' : 'getUsers' }),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).
success(function(data, status, headers, config) {
if(data.success && !angular.isUndefined(data.data) ){
$scope.post.users = data.data;
}else{
$scope.messageFailure(data.message);
}
}).
error(function(data, status, headers, config) {
//$scope.messageFailure(data.message);
});
};
I have also put the files up at plunker The 1.3.16 files at Plunker
I understand that it might be the .success and .error results, but I don't know Angular that much in how to fix it.
I'm a bit of a self-taught coder so any help would be great so I can this group up and running.
Thanks in advance for any advice.
Rod
I recommend you to read this article
Don't use $http's .success()
And
$http({
method: 'post',
url: url,
data: $.param({'user' : $scope.tempUser, 'type' : 'save_user' }),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).
then(function(response) {
your code
excuted when post request is success
},function(response) {
your code
excuted when post request is failed
});
response is returned from server, you can debug to explore more deeply.
For angular 1.6.4 use .then and .catch to deal with the response and the error respectfully:
(note you can save some lines of code by using $http.post)
$scope.init = function(){
$http.post('yourURL', $scope.yourData).
then(function(results) {
if(results.data.success ){
$scope.post.users = results.data;
}else{
$scope.messageFailure(results.data.message);
}
}).
catch(function(results) {
//$scope.messageFailure(results.data.message);
});
};
Thanks everyone for your help, it seems that I really need to take some time out and learn Angular as I really don't understand how it works.
I found that the person before me got the code from AngularJS Insert Update Delete in PHP and MySQL
But it seems that it pops up in a few places on the net as others claiming the code as theirs. Pity about that.
Again thanks for the input but I think I can get it working on ver 1.5.11 and I'll just have to work it out at a later date.
Rod

AngularJS $http POST turn into GET

I have a route
Route::post('/updateLogo', 'CaptivePortalController#updateLogo');
Then I make a POST here
$http({
method: 'POST', <----- I did a POST
url: '/updateLogo',
headers: { 'Content-Type': undefined },
transformRequest: function (data) {
console.log("data coming into the transform is ", data);
var formData = new FormData();
formData.append("company_logo_path", data.files);
console.log($scope.files.company_logo_path);
return formData;
},
data: { files: $scope.files.company_logo_path }
})
.then(function successCallback(response) {
console.log("success");
console.log(response);
$('.save-fade').delay(500).fadeOut(1000);
}, function errorCallback(response) {
console.log("fail");
console.log(response);
});
When I browse the file, and submit the form, I kept getting
405 in my Network tab on Chrome Dev Tool.
Then, I click on it, I see
MethodNotAllowedHttpException in RouteCollection.php line 218:
I know that I'm NOT suppose to make a GET to a POST route, but Why does it make a GET request instead of a POST?
Request URL:http://l.ssc.com:8888/en/updateLogo
Request Method:GET <------
Status Code:405 Method Not Allowed
Remote Address:127.0.0.1:8888
Referrer Policy:no-referrer-when-downgrade
What did do wrong here ?
Any hints ?
This looks like a re-direction taking place.
refer : $http.post() method is actally sending a GET
Please check your route configuration at the server, make sure it is exactly the same as you're requesting.
If you're requesting a '/myroute' but you've defined the route as '/myroute/' then your server could be redirecting to '/myroute'.
All re-directions are done using a GET.
And since the route doesn't allow GET request, it's returning a 405.

How to get URL detail after redirection in angular JS

I am working on angular js application and using below method to get JSON data from the server. This works fine. I have to implement specific scenario, In case of session timeout I need to refresh the page.
The problem is that this application is legacy application and there is a filter which redirect the request to login page if the session is not alive. I am not allowed to change that filter for XMLHttpRequest.
Is there any way for angularJS to detect redirected url by filter.
makeCallPassingJsonObject: function (method, url, data, successCallback, failureCallBack) {
return $http({
method: method,
url: url,
async:true,
data: data,
responseType: 'json',
headers: {
'Content-Type': 'application/json',
'X-Requested-With':'XMLHttpRequest'
}
}).then(function(response){
if(response.status ===200){
successCallback(response.data, response.status, response);
}else if(response.status === 401){
$state.go('gsd.shared.dashboard');
}
else{
failureCallBack(response.data, response.status, response);
}
});
}
Below is the screenshot of browser debuging tool where url is visible. I am not sure how to access that url using angularJS. This url is different from window.location.pathParam

configure angularjs module to send patch request

I am totally new to AngularJs. I am trying to send a PATCH request using Angularjs to Django Tastypie API's. My code is
var module = angular.module('myApp', []);
module.config(function ($httpProvider) {
});
function MyController($scope,$http)
{
$scope.patchCall=function(){
$http({
url: "/patchrequest/",
data:data,
method: "PATCH",
})
.success(function(data){
console.log("SUCCESS");
$scope.list = data.items;
}).error(function() {
console.log("FAIL");
});
}
}
But when I am trying to send a request using this code I am Getting an error that http.patch is not a function. Tell me how can i configure ng-app and services to send a PATCH request using AngularJs. I read PATCH request is available in $resource so i tired it with $resource also. But find the same result. Please guide me how can i configure an app from scratch to send CRUD requests, specially PATCH request
Your error doesn't make sense based on the code you're showing, but a common issue with adding PATCH to AngularJS is that it doesn't have a default Content-Type header for that HTTP method (which is application/json;charset=utf-8 for PUT, POST and DELETE). Here's my configuration of the $httpProvider to add patch support:
module.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.patch = {
'Content-Type': 'application/json;charset=utf-8'
}
}])

Categories

Resources