angular http.post gets sent PHPSESSID cookie ... but its not sent back

612 views Asked by At

I am fairly new to AngularJS (not version 2) and I have a Single Page App that calls (POST) a PHP script on site X, and displays the data. I'm using Angular JS v1.5.7 and I'm not using anything else (eg bootstrap).

It's working ok. I added authentication to it, by using jquery.ajax, and that was fine. I then realised that using JQuery.ajax() with Angular perhaps isn't the best way forward, but have come unstuck in trying to remove the .ajax() call and replace it with $http.

I can see from the debuggers (eg Chrome) that when I do the authentication call, PHPSESSID cookie is not sent to the server. Debugging on the server confirms this. I see the correct reply from the server, and the cookie is included.

I process the reply and the JavaScript goes fetch my data .... but we're not sending the PHPSESSID cookie to the server, so it fails.

If I'd used JQuery ajax() to sent the authentication POST (and $http for all the other calls) it's OK, so it must be related to the $http.post for the authentication.

Here's the snippet:

myAPI.authenticate = function(user, pass) {
   return $http({
       method: "POST",
       url: "authenticate.php",
       withCredentials: true,
       data: "authReq=&userid="+user+"&passwd="+pass,
   })
}

I've also tried:

var app=angular.module('myApp.controllers', [])

app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
}]);

I've seen lots of comments about using withCredentials: true, and tried to set that, both on the request and in the config with setting $httpProvider.default.withCredentials true. Most of this is talking about CORS, but in this case the requests are all to exactly the same page on the same server, so it's not cross origin related.

What am I missing? It must be something to do with the Angular framework because it works ok with JQuery doing the authentication (and $http used for retrieving the data afterwards)

Thanks Monathan

0

There are 0 answers