views:

42

answers:

2

I'm trying to get the success data from a jquery Ajax call so I can use it elsewhere but for some reason its only accessible within the actual success call, so immeditaly below works but the other doesnt'.. any advice is appreciated

      success: function(data) {
        alert (data)
      }

this doesn't work when I try to pass "data" onto another function

    $.ajax({
      type: 'POST',
      url: 'http://localhost/site1/utilities/ajax_component_call_handler',
      data: {
            component_function: component_function,
            param_array: param_array
            },
            dataType: "json",
      success: function(data) {
        receiver (data)
      }
    });

}

my ajax success is calling this:

function receiver (data) {

    ajax_return = data
alert (ajax_return)
}
+2  A: 

Don't use data as a variable name. jQuery objects have an object called data already which holds arbitrary data. If you call your variable dat, you should get better results.

See http://api.jquery.com/jQuery.data/

A shorter implementation could be to just say success: receiver with no parameters, and write your receiver signature as

function receiver(data, textStatus, XMLHttpRequest) {
  /* ... */
}

Then data is passed by the jQuery callback.

sidewaysmilk
There's nothing wrong with using `data` as a variable name. I can't see any conflict with jQuery's `data` method.
patrick dw
thanks, that was it.. odd how jquery uses "data" in their examples, at least thats where I think I got the var name from
Rick
@Rick - Was there some other relevant code you didn't post? All the examples of `data` in your code above are within closures. There shouldn't be any sort of conflict.
patrick dw
nope, it works fine if I just change "data" to "dat"
Rick
In that context, there's a conflict. Because his example assigns `data` an object, then refers to `success: function(data) { receiver(data) };`.A shorter implementation could be to just say `success: receiver` with no parameters, and write your receiver signature as `function receiver(data, textStatus, XMLHttpRequest) { /* ... */ }`. Then `data` is passed by the jQuery callback.
sidewaysmilk
sidewaysmilk - Still doesn't make sense to me. The `data:` in the `xhr` request would be accessible via `this.data`. If there's a conflict, it would seem that doing `alert(data)` as OP did in the first example would result in the same conflict. Even if the `data:` from the `xhr` request was being overwritten, it wouldn't seem to prevent the function from being called. I'm having no issue in identical tests. Oh well.
patrick dw
@patrick dw - I see what you mean. Hrm. The thing that I think looks funny is the `receiver(data)`. I think that this might be interpreted as `receiver({component_function: component_function, param_array: param_array})`, but I'm definitely not sure. :) Because it seems fuzzy, I wouldn't reuse "data."
sidewaysmilk
sidewaysmilk - Well, changing the name did solve the issue for OP. I'd just be curious as to why. I've a feeling that we're missing some code. Even if `receiver()` was called in the manner you suggested in the comment above, the `alert()` would still work. It would just give `[object Object]`. I guess we'll never know for sure what happened. :o)
patrick dw
+1  A: 

Have you tried:

$.ajax({
    type: 'POST',
    url: 'http://localhost/site1/utilities/ajax_component_call_handler',
    data: {
        component_function: component_function,
        param_array: param_array
    },
    dataType: 'json',
    success: receiver
});

Or simply use another variable name other than data as it is already used.

Darin Dimitrov