2012-06-13 140 views
0

你好我有下一个函数,总是返回null作为结果,但服务调用成功分支有任何错误, 我想解决它:1.删除异步属性,2.其他使GetStore函数调用的函数应该处理该函数的结果。我如何以正确的方式做到这一点? ()jQuery ajax返回值到其他函数

谢谢。

function GetStore() { 
     $.ajax({ 
      url: serviceurl + 'GetSometing', 
      type: 'POST', 
      contentType: 'application/json', 
      dataType: "json", 
      async: false, 
      success: function (result) { 
       return result; 
      }, 
      error: function (xhr, description, error) { 
       return null; 
      } 
     }); 
    } 

回答

2

如果你想使它异步,你不能等待它返回一个值,你必须实现回调函数。

function GetStore(success, error) { 
    $.ajax({ 
     url: serviceurl + 'GetSometing', 
     type: 'POST', 
     contentType: 'application/json', 
     dataType: "json", 
     success: success, 
     error: error 
    }); 
} 

GetStore(function(result) { 
    // successful result 
}, function(xhr, description, error) { 
    // an error occurred 
});