getJson问题,它返回未定义 [英] getJson problem, it returns undefined

查看:96
本文介绍了getJson问题,它返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有这个代码

var temp;
if(method==1)
  temp = $("#Words").val();             //get the words from textbox
else{
  $.getJSON("http://localhost/mine/test.js", function(data) {
      temp=data.Words;
  });
}
//do something with temp...but temp is undefined after the execution of else

但是在执行getJson之后,温度是不确定的...如果我放了一个alert(temp),它将继续发生什么事情?我该如何利用temp的价值来继续?

but temp is undefined after the execution of getJson...if i put an alert(temp) it continues whats going on? how can i take the value of temp to continue?

提前谢谢!

推荐答案

这是因为getJSON是ajax请求,而不是同步的.试试这个

it's because getJSON is ajax request, not synchronous. Try this

var temp;
if(method==1) {
  temp = $("#Words").val();             //get the words from textbox
  proc(temp);
} else {
  $.getJSON("http://localhost/mine/test.js", function(data) {
      temp=data.Words;
      proc(temp);
  });
}

function proc(data){
    //do something with temp...but temp is undefined after the execution of else
}

这篇关于getJson问题,它返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆