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)
}
3
  • does your receiver function get called? Have you checked in firebug? Also, are you doing this inbetween script tags or in a plugin/object? Commented Aug 11, 2010 at 17:02
  • Your code should work. Are you sure receiver() is in the proper scope? For example, if the $.ajax() call is outside $(document).ready(function() {...}), but the receiver() is inside, then receiver() will not be visible from where you're calling it. Commented Aug 11, 2010 at 17:03
  • the issue was the var name "data", it was calling the function but not passing the data variable Commented Aug 11, 2010 at 17:09

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

There's nothing wrong with using data as a variable name. I can't see any conflict with jQuery's data method.
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 - 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.
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 - 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.
|
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.