jQuery AJAX。返回值未定义?
我有该代码:
var s, d, p = '';
$.ajax(
{
type: "POST",
url: ajaxurl,
data: {action: "get_info"},
success: function(r)
{
// r contain that json data
// {"s":"long-string","d":"string","p":"string"}
// That served from the server with that header
//
// header('Cache-Control: no-cache, must-revalidate');
// header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
// header('Content-type: application/json');
d = r.d;
s = r.s;
p = r.p;
}
}
);
// And here console return s as undefined
console.log(s);
知道出了什么问题吗?
I have that code:
var s, d, p = '';
$.ajax(
{
type: "POST",
url: ajaxurl,
data: {action: "get_info"},
success: function(r)
{
// r contain that json data
// {"s":"long-string","d":"string","p":"string"}
// That served from the server with that header
//
// header('Cache-Control: no-cache, must-revalidate');
// header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
// header('Content-type: application/json');
d = r.d;
s = r.s;
p = r.p;
}
}
);
// And here console return s as undefined
console.log(s);
Any idea on what's going wrong with that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
$.ajax()
调用只是启动 ajax 操作。然后代码会转到您的console.log
语句,但 ajax 调用尚未返回。因此,s
的值尚未设置。在稍后的某个时刻,ajax 调用会返回您的结果,此时您的回调将被触发。因此,如果您想引用返回的值,您应该在回调中引用变量
s
。更好的方法通常是这样的:
如果你确实需要,你可以在回调之外引用
s
,但如果你这样做,你需要采取一些机制来确保< code>s 已经初始化(即您的 ajax 调用已经返回)。这引入了其他复杂性,例如同步和错误处理,并且可能使您的程序流程相当不可靠。The
$.ajax()
call simply starts the ajax operation. The code then falls through to yourconsole.log
statement but the ajax call hasn't returned yet. Hence the value ofs
has not yet been set.At some point later the ajax call returns your results and at that point your call back is fired. So if you want to refer to the value that is returned, you should reference the variable
s
inside the callback.The better way to do this is normally like this:
If you really need to, you can reference
s
outside of the callback but if you do, you need to put some mechanism in place to make sure thats
has already been initialised (i.e. that your ajax call has already returned). This introduces other complexities such as synchronisation and error handling and can make your program flow rather unreliable.原因是因为当您调用
$.ajax(...);
时它会立即返回。success
属性指定 AJAX 请求完成时调用的回调,并且仅在此处填充s
;在此之前它是未定义的。因此,在启动
success
回调之前,基本上不要对s
执行任何操作。The reason is because when you call
$.ajax(...);
it returns immediately. Thesuccess
property specifies a callback that gets called when the AJAX request finishes, ands
is only populated there; it's undefined before that.So basically don't do anything with
s
before thesuccess
callback is launched.问题是您认为代码按照您布置的方式执行。它实际上在符号上相同于:-
你可能想要的是:-
The issue is you think the code executes in the way you have it laid out. It's actually symatically identical to:-
What you probably want is:-
尝试指定数据类型喜欢
<代码>类型:“POST”,
网址:ajaxurl,
数据类型:'json',
数据:{操作:“get_info”},...
Try specifying data type Like
type: "POST",
...url: ajaxurl,
dataType: 'json',
data: {action: "get_info"},