Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#13526 closed bug (duplicate)

ajax recursion error when adding custom parameter with circular dependency

Reported by: matf@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.9.1
Keywords: Cc:
Blocked by: Blocking:

Description

When adding additional data to ajax request as a custom property (e.g. see accepted answer to http://stackoverflow.com/questions/2542208/how-to-refactor-this-javascript-anonymous-function ) recursion error shows up when this data has circular references.

I've created very simple test case on jsfiddle: http://jsfiddle.net/JbQLc/3/

Please confirm if this is a bug. I have problems passing my tree node to callback (nodes have also reference to 'parent' to help with other operations). In my opinion it shouldn't behave that way - does it try to serialize all properties to string? (if yes why?).

PS. It's also possible to add custom data to jqXHR returned by $.ajax itself (it works OK in that case), but I don't know if this option in cross-browser valid. When adding custom data that way it's accessible from jqXHR in callback instead of 'this'.

Change History (8)

comment:1 Changed 6 years ago by jaubourg

Resolution: duplicate
Status: newclosed

Duplicate of #12227 .

comment:2 Changed 6 years ago by dmethvin

Whatever solution evolves from #12227 will still not reconstitute a JavaScript object with a circular dependency on the other side of the connection.

Of course $.ajax() serializes all properties to string, because a string has to be sent over the connection. Attempt to serialize your object with JSON.stringify() for example and you'll get an error about circular dependencies. So perhaps the outcome of the bug fix is that you get an error from jQuery or silent failure instead of a recursion error.

Last edited 6 years ago by dmethvin (previous) (diff)

comment:3 Changed 6 years ago by matf@…

Please read carefully (or see jsfiddle). I didn't write that I want to send object with circular dependencies over the connection, I know it's impossible.

I want to simply pass some additional data to my 'success' or 'error' callback.

I know that I can create closure, put data and callback into the closure and it will work. However I'm looking for general solution that I can use for every ajax call in my application, and since it can be a lot of them I don't want to have to create closure for every one of them.

comment:4 Changed 6 years ago by jaubourg

Please read the documentation and ask for help on the forums.

comment:5 Changed 6 years ago by anonymous

Documentation lacks information about passing additional data to callback functions (or at least I don't see any).

I will try forums/stackoverflow. Sorry for Your time.

comment:6 Changed 6 years ago by dmethvin

I see the gist of your example now. There is one property (.context) that allows you to pass data. The rest should be valid ajax options and not arbitrary properties.

comment:7 Changed 6 years ago by matf@…

Ok I see it working without any problems with .context (http://jsfiddle.net/JbQLc/4/). I didn't expect it to be 'special' in any way and didn't try it.

Thank for Your help!

PS. Out of curiosity: is it safe to assume that the jqXHR object I'm receiving in callback functions is always the same original object that was returned from $.ajax function ? Storing data there also works.

comment:8 Changed 6 years ago by jaubourg

Yes, the jqXHR will always be the same.

To add to what Dave said, by default ajax will deep extend fields found on the options object except for those listed in the flatOptions global option:

$.ajaxSetup({
    flatOptions: {
        priv2: true
    }
});

Another solution to bypass the automated extension of ajax is to set the field within the beforeSend handler:

    $.ajax({
        url: 'someurl/',
        beforeSend: function() {
            this.priv2 = v2;
        }
    });

You can see both these solutions used in this jsfiddle.

OR, you can use context like Dave suggested.

Last edited 6 years ago by jaubourg (previous) (diff)
Note: See TracTickets for help on using tickets.