#13526 closed bug (duplicate)
ajax recursion error when adding custom parameter with circular dependency
Reported by: | 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 10 years ago by
Resolution: | → duplicate |
---|---|
Status: | new → closed |
comment:2 Changed 10 years ago by
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.
comment:3 Changed 10 years ago by
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:5 Changed 10 years ago by
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 10 years ago by
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 10 years ago by
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 10 years ago by
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.
Duplicate of #12227 .