HTML / JavaScript - 函数返回不对 [英] HTML/JavaScript - function return not right

查看:82
本文介绍了HTML / JavaScript - 函数返回不对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中。当我运行调试时,序列如下所示:

1)在var m = GetArray(VA);

2)返回s; //函数的最后一行

3)在调试器中; //第一个调试器行

4)在调试器中; //第二个调试器行

5)不在函数中。当然,var m上没有返回任何值。

如何解决这个bug?感谢您的咨询。



当调用函数GetArray()时,它总是转到'return s;'首先在调试中

In the code below. When I run debugging, the sequence likes that below:
1) At var m = GetArray("VA");
2) At return s; // last line of the function
3) At debugger; // 1st debugger line
4) At debugger; // 2nd debugger line
5) Out of the function. Of course, no value is returned on var m.
How can this bug be fixed? Thanks for your advisory.

When the function GetArray() is called, it always goes to 'return s;' first in debugging

var m = GetArray("VA");
debugger;        // 1st debugger line

function GetArray(s0) {
  var s;
  $.support.cors = true;
  $.ajax({
 	async: true,
	type: 'Get',
	url: myURL,                  // myURL - a global variable 
	dataType: 'json',
	callbackParamName: 'callback',
	crossDomain: true,
	success: function (res) {
          debugger;         // 2nd debugger line
          s = res;                   // hundreds of records 
	},
	error: function (xhr, status, error) {
	  debugger;
	  console.log(error);
	}
   });
   return s;
}

推荐答案

.support.cors = true ;
.support.cors = true;


.ajax({
async: true
type:' 获取'
url:myURL, // < span class =code-comment> myURL - 全局变量
dataType:' json '
callbackParamName:' callback'
crossDomain: true
成功: function (res){
debugger ; // 第二个调试器行
s = res ; // 数百条记录
},
错误:功能(xhr,状态,错误){
< span class =code-keyword>调试器;
console .log(错误);
}
});
return s;
}
.ajax({ async: true, type: 'Get', url: myURL, // myURL - a global variable dataType: 'json', callbackParamName: 'callback', crossDomain: true, success: function (res) { debugger; // 2nd debugger line s = res; // hundreds of records }, error: function (xhr, status, error) { debugger; console.log(error); } }); return s; }


GetArray函数返回的var没有值,因为尚未发生ajax成功调用。

当Ajax调用成功时,则到达第二个调试器行。



The GetArray function is returning the var s without a value as the ajax success call has not occurred yet.
When the Ajax call is successful then the 2nd debugger line is reached.

The success part of the

的成功部分

这篇关于HTML / JavaScript - 函数返回不对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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