你好我有下一个函数,结果总是返回null,但服务调用成功分支有任何错误, 我想修复它:1。删除异步属性,2。使GetStore函数调用的其他函数应该处理该函数的结果。我怎么能以正确的方式做到这一点? ()
感谢。
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;
}
});
}
答案 0 :(得分: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
});