-2

I can't access the data retrieved from an AJAX request on IE11. I am sending an AJAX request using Jquery:

In Chrome an FF i have a responseText field that i can easily access in the response object.

var ExternalApiService = (function(){
    var getTimeGMT = function(){
        return $.get("{api_path}.php",function(data, textStatus, jqXHR){
            return jqXHR.responseText;
        });
    };
    return{
        getTimeGMT: getTimeGMT
    }
})();

Then, when i call

ExternalApiService.getTimeGMT()

On Chrome and FF the object that i get in response has a responseText field that i can easily access. In IE11 i don't have that field.

Yet, in the IE developer tools, on Network, inside the API call, if i click on the right side on Body => Response body, i can see that i have the text that i need. The API call worked, i just can't access the response body.

How do i access that data?

Ori
  • 21
  • 6
  • It's hard to imagine this code to work in any browser. Where are you returning the result of the callback function to?? And where and when are you checking `responseText`? – Teemu Jan 17 '19 at 15:17
  • @Teemu means, what do you think the `return data;` statement is doing / does and what are you basing your "it's not working" on? – freedomn-m Jan 17 '19 at 15:20
  • 3
    "*There has to be a way to access the data i[sic] got from the AJAX*" - yes, it's right there in `, function(data) {` – freedomn-m Jan 17 '19 at 15:20
  • This is an asynchronicity issue. Quite how this works in any browser I don't know. To solve the problem you need to use the callback properly, as the duplicate I've marked shows you. – Rory McCrossan Jan 17 '19 at 15:42
  • i have edited the original post – Ori Jan 17 '19 at 16:11
  • @Ori Given your update, the duplicate question is correct. You're trying to use an asynchronous function in a synchronous manner. See the stuff about `Promise` on https://api.jquery.com/jquery.get/. – ceejayoz Jan 17 '19 at 16:13

1 Answers1

1

Per the docs, responseText is a parameter of the jqXHR object passed as the third parameter to your success function, not a parameter of data. That said, the response data is all in data - there's rarely a reason to go to the raw jqXHR.responseText value, as you already have it.

$.get('https://{api_path}.php',function(data, textStatus, jqXHR){
     console.log(jqXHR.responseText);
     return data;
});

If you're making cross-domain requests without the correct CORS headers, it may not be available.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • 1
    Is there any explanation for "_Obviously it works well in Chrome and FF_"? – Teemu Jan 17 '19 at 15:30
  • 2
    @Teemu If it's working in those browsers, OP didn't show the actual code they're using. CORS implementations vary between browsers in my experience, and the `https://` in OP's code is further indication it might be a cross-domain request. I'm also curious if OP looked in their error console at all. – ceejayoz Jan 17 '19 at 15:35