1

Can anyone elaborate on what might be going wrong? I have no real clue on what to look for at this point.

sample code:

$.ajax({
    url: uploader.fileupload('option', 'url'),
    context: uploader,
    success: function(response){
        //logging uploader
        console.log(uploader);
        //logging this --> logs the same as logging uploader
        console.log(this);
        //loggin response --> clearly shows a context attribute holding the correct data for this response
        console.log(response);
        //logging response.context --> shows undefined
        console.log(response.context)

        var done = uploader.fileupload('option','done');
        done.call(this, null, response);
    },
    dataType:"json"
})

I'm not used to working with the context attribute in $.ajax() call and i'm suspecting this functionality to cause my issue.

The snippet causes issues in my code some time after the ajax call. I'm pretty sure this has nothing to do with ASYNC problems, since problem allready exists way back in the actual success-callback of the original ajax-request.

I've disabled php-headers which were sent, but it remains the same. I tried this based on another topic which implies something could be wrong with my headers. I've also disabled the dataType attr in $.ajax() but then my response isn't recognised as being json.

Any help or tips on how to debug are much appreciated!

UPDATE: removing the context attribute in the ajax-call does solve the issue. But why does it get appended to my response object (or at least seems to be appended) when console.logging()

Bodybag
  • 318
  • 2
  • 11
  • how does your `done`function look like. i think it should be `done.call(this,response)` but cause you use context you can also do `this.done(response)` – t.niese Jan 11 '13 at 14:53
  • 1
    What does `console.log(JSON.stringify(response))` give you? – Matt Jan 11 '13 at 14:54
  • Are you sure that response is being treated as JSON and being parsed into an object? Perhaps `response` is just a string. (Try doing `console.log(typeof response)` to check) – hugomg Jan 11 '13 at 14:57
  • @Matt -> console.log(JSON.stringify(response)) gives a correct answer and does not hold a context attribute.. – Bodybag Jan 11 '13 at 15:05
  • @missingno -> console.log(typeof response) outputs object – Bodybag Jan 11 '13 at 15:06
  • @t.niese -> valid point of the this.done(response), i've been tweaking and altering code to see if it magically would solve my problem. Also it was a way to learn various syntax. So the snippet provided is in that concept a bit different from the actual snippet, but changing this keeps the issue at hand – Bodybag Jan 11 '13 at 15:19
  • 1
    @Bodybag: because `console.log(typeof response) outputs object` and `console.log(response.context)` shows `undefined` but `console.log(response);` holds the context property, i think you add it somehow later. Like Raffaele said, console shows the actual content of the object, not the one at the time you do console.log so if you do changes to it later before you look at its content you will see these changes in the console. – t.niese Jan 11 '13 at 15:24

1 Answers1

4

I'm not sure if this is your problem because the question itself should be better specified. But looking at the title, it seems the plain old problem with console.log()being asynchronous and passing by reference.

When you call console.log(object) the object is kept as a reference and not printed immediately. After some time, when the logging actually happens, the object may be in a different state (for example as in this case, it may have an attribute).

On the contrary when you log obj.property the call is synchronous and that property is not yet defined.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • apparently i've misread your answer myself xD. But i still think the problem is well explained. By console.log being asynch, i thought you were pointing at running code before the ajax call was complete, hence why it would be undefined. But you meant console.log being async. – Bodybag Jan 11 '13 at 15:39
  • How can you then explain that console.log(object.context) does say it is undefined? like it really is... and why this is not being executed asynchronously? or am i missing something else? Anyhow if i'm understanding this correctly my first conclusion would be alert is better for debugging as that DOES show the actual value at the time of the alert()-call – Bodybag Jan 11 '13 at 15:45
  • @Bodybag The HTTP request has already returned when the callback is executed, no doubt about this. I cannot *explain* what's going on because your snippet can't be run and anyway is not your real code. I just wanted to point out a rather *weird* case – Raffaele Jan 11 '13 at 15:53