繁体   English   中英

Ajax调用未执行

[英]Ajax call not executing

我有以下功能:

Recorder.uploadAudio = function(blob, filename){
      var reader = new FileReader();
      reader.onload = function(event){
        var data;
        data = event.target.result;
        $.post( "upload.php", { fname: filename, data: data } );
      };
      reader.readAsDataURL(blob);
      $.get( "get_files.php", function( data ) {
        $('#clips').html(data);
      });
    }

sript upload.php将文件保存到服务器,get_files.php刷新文件列表(它将所有音频文件加载到一个文件夹中并在列表中显示。)当我调试脚本时,upload.php不加载,它停在data = event.target.result行上。 而且,blob参数可以很好地加载,并且可以代表音频文件。

编辑:这是upload.php代码:

<?php

function is_dir_empty($dir) {
  if (!is_readable($dir)) return NULL; 
  $handle = opendir($dir);
  while (false !== ($entry = readdir($handle))) {
    if ($entry != "." && $entry != "..") {
      return FALSE;
    }
  }
  return TRUE;
}

function get_latest_index($path) {
    $latest_ctime = 0;
    $latest_filename = ''; 

    $d = dir($path);

    while (false !== ($entry = $d->read())) {
      $filepath = "{$path}/{$entry}";
      // could do also other checks than just checking whether the entry is a file
      if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
        $latest_ctime = filectime($filepath);
        $latest_filename = $entry;
      }
    }
    $latest_index = preg_replace("/[^0-9]/","",$latest_filename);
    $latest_index = (int)$latest_index;
    $latest_index++;
    if ($latest_index < "10") {
        $latest_index = "0" . $latest_index;
    }

    return $latest_index;
}

// pull the raw binary data from the POST array
$data = substr($_POST['data'], strpos($_POST['data'], ",") + 1);
// decode it
$decodedData = base64_decode($data);
// print out the raw data,


$path = "audio/"; 
$d = dir($path);

if (is_dir_empty($d)) {
    $fname = "MyRecording00";
}
else{
    $fname = "MyRecording" . get_latest_index($path);
}



$filename = "audio/" . $fname . ".wav";
echo $filename;
// write the data out to the file
$fp = fopen($filename, 'wb');
fwrite($fp, $decodedData);
fclose($fp);
?>

FileReader onload事件是异步的; $.post()$.get()异步返回结果。 尝试在FileReader onload事件中同时包含$.get()$.post() $.post()完整回调中调用$.get() $.post()

      var reader = new FileReader();
      reader.onload = function(event){
        var data;
        data = event.target.result;
        $.post( "upload.php", { fname: filename, data: data } )
        .then(function() {
          $.get( "get_files.php", function( data ) {
            $('#clips').html(data);
          })
        });
      };
      reader.readAsDataURL(blob);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM