【发布时间】:2010-10-11 21:42:45
【问题描述】:
我已将一个常见的 ajax 调用包装到一个函数中。它 ping 一个脚本,返回 JSON。
但是,在我的一生中,我似乎无法让 JSON 对象成为函数的返回值。
一定是我想念的相当简单的东西,但我这辈子都做不出来。
function queryCostCenter(user_id, currency_id, country_id){
var output = null;
var destinations = new Array();
var destination = { qty:1, country: country_id };
destinations.push(destination)
var data = {
destinations : $.toJSON(destinations),
user_id : user_id,
currency_id: currency_id
};
$.ajax({
data: data,
type: 'POST',
url: '/lib/ajax/ajax_prepaid_cost_calculator.php',
success: function(data) {
output = data;
alert(output);
}
});
alert(output);
return json;
}
ajax() 调用内部的 alert() 显示 json 对象,但是如果在函数外部尝试和警告,和/或从 ajax() 调用内部返回响应,其值为 null?!
任何帮助/指针将不胜感激。
【问题讨论】:
标签: javascript jquery scope