In the following, the return statement is being executed before the call to $.get() finishes. How can I change this?
var aft = {};
aft.getToken = function (url) {
var theToken;
//Get the token value
$.get(url, function (data) {
theToken = $(data).find('input[name=__RequestVerificationToken]').val();
}, "html");
return theToken;
};
You can’t—at least not sensibly. While there is an
asyncproperty you can set on jQuery ajax requests, I’ve had serious problems trying to use it with afalsevalue in the past.Try to re-think what you’re trying to accomplish:
And then:
Or, if your callback only needs access to the returned data, then you could more simply do
And then