小编use*_*715的帖子

javascript/jquery范围让我难过

我已将一个常见的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;

}
Run Code Online (Sandbox Code Playgroud)

ajax()调用中的alert()显示json对象,但是如果在函数外部尝试并发出警报,和/或从ajax()调用内部返回响应,则其值为null?!

任何帮助/指针将不胜感激.

javascript jquery scope

2
推荐指数
1
解决办法
461
查看次数

标签 统计

javascript ×1

jquery ×1

scope ×1