Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS: http.post is returning null in Internet Explorer

I'm making an ajax call from Angular JS:

var response = $http.post(
    '/services/login/?_nochache='+new Date().getTime(),      
    JSON.stringify(credentials)
);

I'm adding the _nocache setting, thinking that maybe some cache or something like that.

I'm also converting the object credentials into string thinking that Internet Explorer could not recognise the object.

I'm really lost here, In chrome the call works perfectly, in IE 10, the response of the service is null.

What can be causing this?

EDIT

The service is returning 401, which is ok, since the user is wrong, but the response should be (as is in other browsers), the error string saying the user is wrong, in this case is null.

I'm using the promise like this:

promise.then(onLoginOk, onLoginError);
...

function onLoginError(response) {
   console.log(JSON.stringify(response));
}

The console returns

{
  "data": null,
  "status": -1,
  "config": {
    "method": "POST",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "url": "http://dev.site.com:8000/api/auth/login/",
    "data": {
      "username": "[email protected]",
      "password": "password"
    },
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/json;charset=utf-8"
    }
  },
  "statusText": ""
}

EDIT

Here is the Response body I get in IE. enter image description here

These are the headers I get with the 401 which is correct, but the response body is wrong. enter image description here

like image 265
Pablo Avatar asked Aug 15 '16 22:08

Pablo


1 Answers

This seems like same problem of mine.

In my case, my request function works well in every browser except MS Edge.

I tried to find solution or reason, and finally I got one.

Some answers suggested adding tags like

<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="Mon, 26 Jul 1997 05:00:00 GMT" />

or adding code into config like

$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
// extra
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';

but these couldn't solve my problem.

Finally I saw one answer said this may occurred by a kind of policy handling caches in Edge.

This can occur when I use local server to send requests or get responses.

And it will gonna disappear when test on other server not on local.

I hope your problem is same as mine.

like image 194
Canet Robern Avatar answered Oct 22 '22 15:10

Canet Robern