最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery, passing success data from AJAX to another function? - Stack Overflow

matteradmin13PV0评论

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_ponent_call_handler',
      data: {
            ponent_function: ponent_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)
}

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_ponent_call_handler',
      data: {
            ponent_function: ponent_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)
}
Share Improve this question edited Aug 11, 2010 at 16:56 bakkal 55.5k12 gold badges136 silver badges113 bronze badges asked Aug 11, 2010 at 16:55 RickRick 17k35 gold badges113 silver badges163 bronze badges 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? – hvgotcodes 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. – user113716 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 – Rick Commented Aug 11, 2010 at 17:09
Add a ment  | 

2 Answers 2

Reset to default 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./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.

Have you tried:

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

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far