最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论