Side navigation
#13526 closed bug (duplicate)
Opened February 27, 2013 02:21PM UTC
Closed February 27, 2013 02:26PM UTC
Last modified February 28, 2013 12:56AM UTC
ajax recursion error when adding custom parameter with circular dependency
Reported by: | matf@op.pl | 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'.
Attachments (0)
Change History (8)
Changed February 27, 2013 02:26PM UTC by comment:1
resolution: | → duplicate |
---|---|
status: | new → closed |
Changed February 27, 2013 02:32PM UTC by comment:2
_comment0: | 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.parse()` 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. → 1361975579072569 |
---|
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.
Changed February 27, 2013 11:53PM UTC by comment:3
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.
Changed February 27, 2013 11:59PM UTC by comment:4
Please read the documentation and ask for help on the forums.
Changed February 28, 2013 12:16AM UTC by comment:5
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.
Changed February 28, 2013 12:20AM UTC by comment:6
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.
Changed February 28, 2013 12:54AM UTC by comment:7
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.
Changed February 28, 2013 12:56AM UTC by comment:8
_comment0: | 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` gloabl option: \ \ {{{#!js \ $.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 [http://jsfiddle.net/Gpxun/ this jsfiddle]. \ \ OR, you can use `context` like Dave suggested. → 1362013014718607 |
---|
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 .