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 thereceiver()
is inside, thenreceiver()
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
2 Answers
Reset to default 3Don'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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742371161a4431293.html
评论列表(0条)