I need to return true or false to my alert() from the results of an ajax success function. However, I'm having problems since the alert() result is always 'undefined'...any ideas on how to fix this?
我需要从ajax成功函数的结果返回true或false到我的alert()。但是,我遇到了问题,因为alert()结果总是“未定义”...有关如何解决此问题的任何想法?
I have DoAjax
as it's own function because I call it multiple times within the .js file.
我有DoAjax作为它自己的函数,因为我在.js文件中多次调用它。
var val = '12345';
alert( DoSerialVerification( val ) ); //this returns 'undefined' regardless of cType.length
function DoSerialVerification( piVal ){
var fSuccess = function( oData ){
var cType = oData.type
if( cType.length > 0 ){
alert( cType ); //with piVal = 12345 cType will return 'MODULE'
$('#dialog-1').attr('type', cType);
return true;
} else {
return false;
}
};
DoAjax({ Action: "DoSerialVerification", Value: piVal }, fSuccess );
}
function DoAjax( pAjaxParams, pSuccess ){
$.ajax({url : 'procedures?',
data : pAjaxParams,
async : false,
type : "POST",
dataType : "json",
error : function(oD,oT,oE){ alert( oD+'\n'+oT+'\n'+oE ) },
success : pSuccess
});
}
2 个解决方案
#1
1
you need to return something from the DoSerialVerification function... modified DoSerialVerification function:
你需要从DoSerialVerification函数返回一些东西...修改DoSerialVerification函数:
function DoSerialVerification( piVal ){
// here
var result = false;
var fSuccess = function( oData ){
var cType = oData.type
if( cType.length > 0 ){
alert( cType ); //with piVal = 12345 cType will return 'MODULE'
$('#dialog-1').attr('type', cType);
//here
result = true;
}
};
DoAjax({ Action: "DoSerialVerification", Value: piVal }, fSuccess );
//here
return result;
}
#2
0
What you're trying to do isn't possible: calling AJAX is an asynchronous process, your function DoAjax will return before the response is ever received from the server. That is why you are providing callbacks: once the server returns a response, it'll enter either your error callback or your success callback, depending on the result.
您尝试做的事情是不可能的:调用AJAX是一个异步过程,您的函数DoAjax将在从服务器收到响应之前返回。这就是您提供回调的原因:一旦服务器返回响应,它将输入您的错误回调或成功回调,具体取决于结果。
Change this:
改变这个:
alert( DoSerialVerification( val ) );
To this:
对此:
DoSerialVerification(val);
And then move your alert call into your "fSuccess" function.
然后将警报调用移至“fSuccess”功能。
#1
1
you need to return something from the DoSerialVerification function... modified DoSerialVerification function:
你需要从DoSerialVerification函数返回一些东西...修改DoSerialVerification函数:
function DoSerialVerification( piVal ){
// here
var result = false;
var fSuccess = function( oData ){
var cType = oData.type
if( cType.length > 0 ){
alert( cType ); //with piVal = 12345 cType will return 'MODULE'
$('#dialog-1').attr('type', cType);
//here
result = true;
}
};
DoAjax({ Action: "DoSerialVerification", Value: piVal }, fSuccess );
//here
return result;
}
#2
0
What you're trying to do isn't possible: calling AJAX is an asynchronous process, your function DoAjax will return before the response is ever received from the server. That is why you are providing callbacks: once the server returns a response, it'll enter either your error callback or your success callback, depending on the result.
您尝试做的事情是不可能的:调用AJAX是一个异步过程,您的函数DoAjax将在从服务器收到响应之前返回。这就是您提供回调的原因:一旦服务器返回响应,它将输入您的错误回调或成功回调,具体取决于结果。
Change this:
改变这个:
alert( DoSerialVerification( val ) );
To this:
对此:
DoSerialVerification(val);
And then move your alert call into your "fSuccess" function.
然后将警报调用移至“fSuccess”功能。