24

I am making an ajax call to a backend rest api, the api is returning fine. If I console.log() the success data and error data, it gives "resource logged in", 200 ok on the console but when I view it in the network tab response for that auth/login route, it shows "Failed to load the response data". And this happens sometimes only and not always. Why? Here's the snippet of my ajax call.

ajax
    .post('auth/login', {
          data: {
              oauth_provider: 'google',
              oauth_token: (isToken ? authResult : authResult.access_token)
              },
          cache: false
          })
          .done(function(data) {
             console.log(data); // Resource Logged in
           })
           .error(function(err){
            console.log(err);
           })

Here's the content of my ajax.js

define(
  [
    'jquery',
    'util',
  ],
  function ($, util) {
    var ajax = {
      request: function (type, url, options) {
        if (url.indexOf('http') === -1) {
          url = util.url(url);
        }

        if (options === undefined) {
          options = {};
        }

        options.type = type
        options.url = url;

        return $.ajax(options);
      },

      get: function (url, options) {
        return ajax.request('GET', url, options);
      },

      post: function (url, options) {
        return ajax.request('POST', url, options);
      },

      put: function (url, options) {
        return ajax.request('PUT', url, options);
      },

      delete: function (url, options) {
        return ajax.request('DELETE', url, options);
      }
    };

    return ajax;
  }
)

enter image description here enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Param Singh
  • 1,325
  • 3
  • 13
  • 28
  • 1
    Typically this is from the response being blocked because it is a cross-domain request. Is your page an the AJAX content on the same server? – scunliffe Sep 28 '15 at 11:03
  • Yes, its on the same server. I'm using Nginx. – Param Singh Sep 28 '15 at 11:04
  • Possible duplicate of [Chrome dev tools fails to show response even the content returned has header Content-Type:text/html; charset=UTF-8](https://stackoverflow.com/questions/38924798/chrome-dev-tools-fails-to-show-response-even-the-content-returned-has-header-con) – dhilipsiva Jul 03 '17 at 11:48

7 Answers7

5

Apparently, it turns out that there's some problem with the clearing up the cookies. On clearing up them , the system behaves fine. Not sure need help!

Param Singh
  • 1,325
  • 3
  • 13
  • 28
2

This is a duplicate of: Chrome dev tools fails to show response even the content returned has header Content-Type:text/html; charset=UTF-8

For anyone coming here in the future, here is your answer: https://stackoverflow.com/a/38925237/1235072

More info: https://bugs.chromium.org/p/chromium/issues/detail?id=141129

dhilipsiva
  • 3,688
  • 1
  • 24
  • 35
1

I confirm this. Since Chrome version 45 and I see some of my Ajax requests got 200 as a status code but a problem in showing the content "Failed to Load Content".

My Ajax requests are inside for loop, request the failed contents again makes the content to be loaded fine.

It seems to solve that inside the for loop is using setTimeout between request and the other.

Mohammed AlBanna
  • 2,350
  • 22
  • 25
0

An "Empty Cache and Hard Reload" worked for me.

RyanOC
  • 587
  • 5
  • 16
0

it was fixed when I change display errors to false in my php ajax page, this looks like it too.
From ini_set('display_errors', 1); to ini_set('display_errors', 0);

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

I am using Javascript and if I go with this approach, it will take longer time and wont get any data from the network tab:

         xhr.onreadystatechange=function(){
          if(xhr.readyState==4){...}}

Since you are working on getting the onreadystatechange and readyState, that takes time and the browser deletes the information before you get it. So by using this, you will get the text response:

         xhr.addEventListener("load", (function(){ console.log(this.responseText);}));

OR

         xhr.onload = function(){ console.log(this.responseText);};

EDIT:

In my PHP file, I am using this headers:

header("Cache-Control: no-cache, must-revalidate"); //
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); //Date in the past
JoSe ToTo
  • 1
  • 1
-3

Because of postback this was happening in my project. When I solved the postback issue,the error gone.

Nupur
  • 17
  • 1