0
我調用另一個javascript函數,如下所示,但每次isPassword被調用時,它都將返回爲'undefined',而不是True或False。我在打印返回的值之前打印這些值,但這些值是正確的,但返回true;不起作用。有任何想法嗎?當我在Javascript中返回true時,它將返回爲undefined
function isPassword(pass){
var datatosend = {};
datatosend['api'] = 'checkpassword';
datatosend['version'] = 'v1';
datatosend['pass'] = pass;
jsonAPI(datatosend,function()
{
if (typeof(responseVar['success'])=='boolean')
{
var ans = responseVar['success'];
if (ans)
{
//Returns true to changePassword();
return true;
}
else if (!ans)
{
//Returns false to changePassword();
return false;
}
}
});
}
function changePassword(){
//THE BELOW isPassword CALL IS UNDEFINED
if(isPassword(oldPass)){
successStatus.text("Current password not entered correctly");
successStatus.css('color', 'red');
clearTextAfter('changePasswordStatus', 5000);
return false
}
}
我可以根據要求提供更多的代碼,如果它不僅僅是一個簡單的問題。
您正在使用'responseVar',但它沒有在您展示的代碼中的任何位置定義。你使用了一種名爲'jsonAPI'的東西,是一種異步方法嗎? – Guffa
您正在從changePassword調用isPassword - 如果它返回false - 它只會返回undefined(因爲您的返回在if語句的範圍內)。 在changePassword內的if語句之後放置返回True,或返回False - 無論適合什麼。 – Jordan
「oldPass」的定義在哪裏? –