1

This seems like the most bizarre thing:

var mah_dataz = $.get("link/to/request");
console.log(mah_dataz);

/* result is the whole shebang: 
  Object {
    abort: function (a){var b=a||u;return d&&d.abort(b),c(0,b),this}
    always: function (){return e.done(arguments).fail(arguments),this}
    complete: function (){if(i){var c=i.length;!function f(b){ab.each(b,function(b,c)
    readyState: 4
    ... you get the idea...
    responseText: "{'returns': {'wellFormatted':'JSON', 'cross':'MyHeart'}}" */

But!

var mah_dataz = $.get("link/to/request");
console.log(mah_dataz.responseText)
// result is: 
// undefined

This was very quickly marked a duplicate and dismissed, but no one even fully answered the question let alone posted a link to the duplicate. The question is:

Why is this so? Why is a whole object returned in the first case, and then, in the second case when a property thereof is referenced, it's undefined? I don't understand why this kind of object behaves fundamentally (or appears to so behave) differently than other javascript objects?

Jonline
  • 1,677
  • 2
  • 22
  • 53
  • To those who chose to mark this as a duplicate and not even read the actual question, please either reopen the question or answer it, because I am absolutely, absolutely stuck and going crazy from what appears, from my eager but early eyes, to be javascript taking a detour from sanity. I've gone over the answer you've linked; I don't see how it answers my question (I can see that it has a lot of background info, and answers related questions, but not *my actual question*). – Jonline May 19 '14 at 22:05
  • 1
    It is [`console.log` that mocks you](http://stackoverflow.com/a/23392650/1048572). The `responseText` property is `undefined` when you access it, but has the data when you look at the object in the console. – Bergi May 19 '14 at 22:09
  • Ok, *thankyou* enormously, I am just furious with @BenjaminGruenbaum, who, despite being an obviously important contributor, has utterly dismissed my actual problem today. I do see how this now becomes an issue described in the linked answer, thank YOU for taking the time to help me get to it. – Jonline May 19 '14 at 22:11
  • 1
    Actually I would have closed it for that duplicate as well after having commented on the `console.log` issue. I wonder whether we should incorporate this case in the canonical question. – Bergi May 19 '14 at 22:13
  • 1
    Well, as I understand it—now—it is, implicitly, addressed by the cannonical question. But it's the format, or perhaps the presentation of the problem which is at issue; the canonical question explains what's happening in a way that's not obviously pertinent to the actual experience of the problem as I describe it (namely, what appears to be an object with unreachable properties). Finding a way to connect the semblance of this problem with the reality of the solution would, imo, certainly make this a duplicate. – Jonline May 19 '14 at 22:20

1 Answers1

2

Not quite. The get returns a deferred not the result of the callback

var mah_dataz;
var deferred = $.get("link/to/request", function(jqxhr_ob) { mah_dataz = jqxhr_ob});

if you're just after the response you could do...

$.get("link/to/request")
    .done(function(response) {
        console.log(response);
    });

Which is even tidier.

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • Tried both of these, and in neither case does the result I want obtain. – Jonline May 19 '14 at 22:06
  • What does it actually print to the console? And what do you want it to print? – phuzi May 19 '14 at 22:10
  • Undefined, undefined, undefined—but @bergi, in commenting on my question, has finally pointed me in the right direction on that front. Thanks, though. – Jonline May 19 '14 at 22:12
  • Could be because of the typo if you copied verbatim. Oops, spelled response incorrectly in the callback. Have updated, my bad :o( – phuzi May 19 '14 at 22:13
  • 1
    No, it really is the problem as @bergi described; it really is the case that console.log() is lying to me. It's a question of synchronicity, I can follow it now, but it certainly *looks* bizarre to a non-expert in AJAX. – Jonline May 19 '14 at 22:18
  • Yeah, you'll need to wait for the request to complete before you try and access the result. Using the Deferred.done though you get the responseText as the argument to the callback, in this example `response` – phuzi May 19 '14 at 22:20
  • Hrmm... will try again, but even with .done() I was still getting undefined.. will mark your answer as right if a) the duplicate status is removed and b) I can get it working. ;) – Jonline May 19 '14 at 22:21