-1
我在自定義函數中有ajax函數,我需要調用我的自定義函數並從ajax函數中獲取一些變量。爲什麼這是不可能的?Javascript:功能中的函數並返回
1想法:
<script>
function getValue(){
$.nette.ajax({
url: "http://example.com/request",
contentType: 'application/json',
dataType: 'json',
success: function(payload) {
console.log(payload.value); // contains RIGHT value
return payload.value;
}
});
}
var try = getValue();
console.log(try); // = undefined
</script>
第二個想法:
<script>
function getValue(){
var returning = "";
$.nette.ajax({
url: "http://example.com/request",
contentType: 'application/json',
dataType: 'json',
success: function(payload) {
returning = payload.value;
console.log(payload.value); // contains RIGHT value
}
});
console.log(returning); // = ""
}
</script>
*在異步調用完成之前正在執行'console.log(返回)'。 –