gpt4 book ai didi

javascript ajax 在浏览器控制台中返回值 "stolen"

转载 作者:行者123 更新时间:2023-11-30 17:48:10 25 4
gpt4 key购买 nike

我在浏览器中执行了以下 javascript 代码:

function run(request) {
var res;
$.ajax({
url:'http://custom-host:8080/',
type: "POST",
async: false,
data: request
}).done(function(data, textStatus, jqXHR){
console.log(textStatus);
res = data;
});
return res;
}

它只是向我的自定义服务器请求它得到的响应。 Chrome 控制台日志如下所示:

> var a = run({command:'version'}); // executing custom function
success dev.pycached-admin/:14 // this is the console log
undefined // this is the console returned value
> a // ask for value a
"1.1" // value a (returned from ajax)

问题是:当之后返回“1.1”的实际值(值已正确分配)时,为什么会在控制台中返回 undefined?

如果我在 done 函数中添加 debugger 语句:

}).done(function(data, textStatus, jqXHR){
console.log(textStatus);
debugger;
res = data;
});

然后我可以看到可能维护 chrome 控制台的 Apple 代码(脚本选项卡中的 VM 文件)。反正AJAX调用是同步的,为什么第一次没有返回值呢?

最佳答案

...so why is the value not returned for the first time?

是的,但是 var 是一个语句,而不是一个表达式,所以它不能有一个结果供控制台显示。如果你这样做,你可以看到这个:

> var a = "foo";undefined> a"foo"

所以就这样做:

> var aundefined> a = run({command:'version'});

...这应该给你:

success dev.pycached-admin/:14 // this is the console log"1.1"

关于javascript ajax 在浏览器控制台中返回值 "stolen",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19677461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com