Ajax is asynchronous, i.e. when return links
is executed, the callback function in success
might not even have been called.
Extend your function to accept a callback:
function Favorites(callback) {
var links;
$.ajax({
type: "GET",
url: "/Services/Favorite.svc/Favorites",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: callback
});
};
and call it with:
var callback = function(msg) {
links = (typeof msg.d) == 'string' ? eval('(' + msg.d + ')') : msg.d;
// do other stuff with links here
}
Favorites(callback);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…