2013-05-07 62 views
0

我的jQuery Form插件中的beforeSubmit函数需要检查所选文件是否已存在于服务器上。下面是相关代码:由jQuery Form Plugin的beforeSubmit不返回值调用的函数

$('#frmSermonUpload').ajaxForm({ 
    beforeSubmit: function() { 
     // Reset errors and clear messages 
     ClearForm(false); 
     var formValid = true, 
      fileExists = CheckFileExists(); 

     console.log('beforeSubmit fileExists: ' + fileExists); 

     if (fileExists === 'true') { 
      $('#uploadedFile').addClass('inputError'); 
      $('#fileErrorMsg').append(' A file with that name already exists on the server.'); 
      formValid = false; 
     } else { 
      if (!ValidateUploadForm()) { 
       formValid = false; 
      } 
     } 

     console.log('formValid: ' + formValid); 

     if (!formValid) { 
      return false; 
     } 

    }, 
    ... 

这里的CheckFileExists()函数:

function CheckFileExists() { 

    var fileName = $('#uploadedFile').val().replace(/C:\\fakepath\\/i, ''), 
     dataString; 

    dataString = 'checkFileExists=' + fileName; 

    console.log('fileName: ' + fileName); 
    console.log('dataString: ' + dataString); 

    $.ajax({ 
     type: 'POST', 
     url: '../scripts/sermonUpload.php', 
     data: dataString, 
     success: function(serverResult) { 
      console.log('serverResult: ' + serverResult); 

      if (serverResult === 'existsTrue') { 
       return 'true'; 
      } else { 
       return 'false'; 
      } 

     }, 
     error: function(xhr, status, error) { 
      alert('An error occurred while attempting to determine if the selected file exists. Please try again.); 
     } 
    }); 

    //console.log('Current value of returnResult: ' + returnResult); 
    //return returnResult; 
} 

正如你可以看到我使用控制台输出来检查这是怎么回事。在CheckFileExists()函数中,正在报告fileName和dataString。在PHP方面,我知道由于某些日志记录,POST数据已经到达那里。

下面是一个使用POST数据的PHP代码:

if (isset($_POST['checkFileExists']) && $_POST['checkFileExists'] !== '') { 
    $log->lwrite('**Checking if file exists.**'); 

    $fileToCheck = $targetPath . $_POST['checkFileExists']; 

    $log->lwrite('file_exists: ' . file_exists($fileToCheck)); 

    if (file_exists($fileToCheck)) { 
     echo 'existsTrue'; 
    } else { 
     echo 'existsFalse'; 
    } 
} 

发生了什么事,在控制台,行console.log('beforeSubmit fileExists: ' + fileExists);将返回 “未定义”(beforeSubmit fileExists: undefined)。

这里的所有控制台输出该文件已经存在一个上传的,所以beforeSubmit应停止:

fileName: 042913sermon.mp3 dataString; checkFileExists=042913sermon.mp3 beforeSubmit fileExists: undefined formValid: true serverResult: existsTrue

它必须是显著的serverResult在其他所有内容后,行显示。这是否与ajax通话需要多长时间有关?如果是这样,有没有办法延迟脚本的其余部分,直到ajax调用执行完毕?

UPDATE 作为aorlando指出,控制台输出的顺序标志着我需要添加async: false我的$就调用。这样做后,控制台输出是正确的,但函数CheckFileExists()仍然报告为beforeSubmit中未定义。

回答

1

好的。现在问题是回报的范围。 如果使用“异步:假”,你可以用这种方式(不那么优雅)

var returnValue=''; 
$.ajax({ 
     type: 'POST', 
     url: '../scripts/sermonUpload.php', 
     data: dataString, 
     async: false, 
     success: function(serverResult) { 
      console.log('serverResult: ' + serverResult); 

      if (serverResult === 'existsTrue') { 
       returnValue = 'true'; 

      } else { 
       returnValue= 'false'; 
      } 

     }, 
     error: function(xhr, status, error) { 
      alert('An error occurred while attempting to determine if the selected file       exists. Please try again.); 
     } 
    }); 
return returnValue; 

您必须声明一个变种的returnValue出Ajax调用的范围返回。在ajax函数内部,你可以修改returnValue的值; 这是一个使用闭包的解决方案,这是一个非常复杂的javascript功能。进一步阅读关于javascript中变量范围的内容:What is the scope of variables in JavaScript?

这不是一个很好的解决方案;如果你在前面的例子中调用ajax调用的“成功”函数中的函数,那么它会更好。

这就是所有人!

+0

这样做!感谢一百万,奥兰多! – marky 2013-05-07 17:37:01

1

您正在使用AJAX异步调用。 您的方法CheckFileExists()n在ajax调用完成之前返回一个值。 因此最简单的解决方案是使用:

$.ajax({ 
     type: 'POST', 
     url: '../scripts/sermonUpload.php', 
     data: dataString, 
     async: false ... 

,如果你想使用异步调用(默认值,你可以看到:http://api.jquery.com/jQuery.ajax/ 必须调用(为前)一个postcall功能的成功功能Ajax调用:

success: function(serverResult) { 
     console.log('serverResult: ' + serverResult); 

     if (serverResult === 'existsTrue') { 
      postFn('true'); 

     } else { 
      postFn('false'); 
     } 

    }, ... 

要当心用的postFn

funcion postFn(_result){ 
    console.log(_result); 
} 

我希望是明确的范围。这就是所有人!

+0

谢谢,奥兰多。当您发布您的答案时,我注意到了异步问题。我将很快更新我的问题,以反映以下内容:添加'async = false'后,控制台输出按正确的顺序显示,但CheckFileExists()在beforeSubmit中报告为undefined。 – marky 2013-05-07 15:58:02