日期:2014-05-17  浏览次数:20771 次

主函数获取不了AJAX返回值 ,老是为 undefined
[code=JScript]
function   shenQ(){

var   checkJL=checkjianli();
                                alert(checkJL);     //这里的返回值老是为   undefined

if   (checkJL== "ok "){
window.shengqingjobtitle.innerHTML=SjobTitle;
document.getElementById( "ShengQingJobId ").value=SjobId;
}
else   if(checkJL== "no "){
window.shengqingjobtitle.innerHTML=SjobTitle;
document.getElementById( "shengqingjob ").value= "简历不完整,请修改简历 ";
document.getElementById( "shengqingjob ").disabled=true;
}
else{
alert( "发生意外,请联系管理员 ");
closeDiv();
}
}
}

function   checkjianli(){
var   xmlhttp;
try{
xmlhttp=new   XMLHttpRequest();
}
catch(e){
xmlhttp=new   ActiveXObject( "Microsoft.XMLHTTP ");
}
xmlhttp.onreadystatechange=function(){
if   (xmlhttp.readyState==4){
if   (xmlhttp.status==200){
var   data=xmlhttp.responseText;
alert(data);   //这里正确
return   data;
}
else{
alert( "失败,请联系管理员 ");
}
}
}
xmlhttp.open( "post ",   "checkjianli.asp ",   true);
xmlhttp.setRequestHeader( 'Content-type ', 'application/x-www-form-urlencoded ');
xmlhttp.send( "lx=1 ");
}

[/code]

------解决方案--------------------
因为AJAX是异步的,就相当于多线程一样。
因此你要使用它返回的值,必须使其同步。
xmlhttp.open( "post ", "checkjianli.asp ", false); 
改成false使用同步方式就行了。
------解决方案--------------------
探讨
因为AJAX是异步的,就相当于多线程一样。
因此你要使用它返回的值,必须使其同步。
xmlhttp.open( "post ", "checkjianli.asp ", false);
改成false使用同步方式就行了。

------解决方案--------------------
探讨
因为AJAX是异步的,就相当于多线程一样。
因此你要使用它返回的值,必须使其同步。
xmlhttp.open( "post ", "checkjianli.asp ", false);
改成false使用同步方式就行了。

------解决方案--------------------
alert(checkJL); //这里的返回值老是为 undefined 

if (checkJL== "ok "){ 
window.shengqingjobtitle.innerHTML=SjobTitle; 
document.getElementById( "ShengQingJobId ").value=SjobId; 

else if(checkJL== "no "){ 
window.shengqingjobtitle.innerHTML=SjobTitle; 
document.getElementById( "shengqingjob ").value= "简历不完整,请修改简历 "; 
document.getElementById( "shengqingjob ").disabled=true; 

else{ 
alert( "发生意外,请联系管理员 "); 
closeDiv(); 


 把这段代码移到if (xmlhttp.status==200){ 
var data=xmlhttp.responseText; 
alert(data); //这里正确 
return data; 

这里面去就可以了.也可以把这段代码写到一个方法里.在这里面去调用那个方法就行了.要得到异步调用的返回值,必须等响应回来之后才能进行处理.