日期:2014-05-16  浏览次数:20308 次

JS结合Ajax获取function的返回值,求教!!!!
用到Ajax的function代码如下:
此function的作用是根据传过来的类别ID,返回它下面的直属子类别信息。想返回的值就是resultStr
function GetChildClassStr(classID){
  createXMLHttpRequest();
  ajax.open("POST", "../Ajax/showClassList.ashx", true);
  ajax.onreadystatechange = function() {
  if (ajax.readyState == 4) {
  if (ajax.status == 200) {
  var resultStr = ajax.responseText;
  return resultStr;
  }
  else {
  alert(ajax.status);
  }
  }
  }
  ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  ajax.send("type=child&id=" + classID);
}

以下是另外一个调用GetChildClassStr方法的function,代码如下:
function AddChildNode(classID){
  var childClassStr=GetChildClassStr(classID);// 字符串格式"||1000-五金用品||1001-电脑设备"
  var childClasses=childClassStr.split("||");
  ......
}

现在的问题是,每次在function AddChildNode里获取到的childClassStr值为undifined。
经过调试脚本后发现,function GetChildClassStr在每次执行到ajax.readyState == 4的时候会跳出方法,去执行下面的语句,然后不知道是什么时候又会执行到ajax.status == 200的if条件里面去,然后得到的resultStr 值是正确的。但是在它跳出方法执行下面的语句的时候已经报错了,因为function AddChildNode的 childClassStr还没有得到值,然后去做split,报错了。
此问题要如何解决呢?

------解决方案--------------------
这是异步的。没法控制数据的先后。楼主要换种思路实现。何不把逻辑写在后台呢?
------解决方案--------------------
要么在ajax的onreadystatechange事件函数中处理你的逻辑,要么把ajax改为同步。
------解决方案--------------------
要不选择同步,要不在回调函数里面执行后面需要的操作。
------解决方案--------------------
在GetChildClassStr这个涵数中定义一个局部变量
我大概写一个伪代码
function GetChildClassStr()
{
var s="";
ajax.onreadystatechange = function() {
//在这里把返回值赋给s,即是把return resultStr;改为s=resultStr;
}

//在ajax请求完成的后的外面。将resultStr返回;即GetChildClassStr()这个涵数的最后一行代码是
return s;
}

这样你在其他涵数调用GetChildClassStr()就能取到返回值了
------解决方案--------------------
下面是我自己封装的ajax和部分代码,希望能帮到你
JScript code

 
Object.prototype.copy=function(extendObj){
    if(!App.isObject(this))throw {message:'对象不支持此属性或方法'};
    if(App.isObject(extendObj))for(var p in extendObj)this[p]=extendObj[p];
}
Object.prototype.copyIf=function(extendObj){
    if(!App.isObject(this))throw {message:'对象不支持此属性或方法'};
      if(App.isObject(extendObj))for(var p in extendObj)if(App.isUndefined(this[p]))this[p]=extendObj[p];
}
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'')};

var App={
      _getType:function(v){return Object.prototype.toString.apply(v)},
      getType:function(v){if(v===undefined)return '[object Undefined]';else if(v===null)return '[object Null]';return App._getType(v)},
      isFunction:function(v){return App._getType(v)==='[object Function]'},
      isObject:function(v){return App._getType(v)==='[object Object]'},
      isArray:function(v){return App._getType(v)==='[object Array]'},
      isDate:function(v){return App._getType(v)==='[object Date]'},
      isString:function(v){return App._getType(v)==='[object String]'},
      isBoolean:function(v){return App._getType(v)==='[object Boolean]'},
      isNumber:function(v){return App._getType(v)==='[object Number]'},
      isInt:function(v){return App.isNumber(v) && (v+'').indexOf('.')<0},
      isUndefined:function(v){return v===undefined},
      isNull:function(v){return v===null},
      isBlankString:function(v){return App.isString(v) && v.trim().length==0},
      isNotBlankString:function(v){return App.isString(v) && v.trim().length>0},
      isEmptyObject:function(v){return App.isOb