Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Wordpress中通过Ajax调用更新链接后,Javascript代码不起作用_Javascript_Php_Jquery_Wordpress - Fatal编程技术网

在Wordpress中通过Ajax调用更新链接后,Javascript代码不起作用

在Wordpress中通过Ajax调用更新链接后,Javascript代码不起作用,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我正在建立一个包含文件存储的wordpress网站。 布局如下: 左侧有一个侧栏,显示完整的目录结构。 在侧边栏旁边的右侧有一个文件列表,其中包含作为链接的文件名。 如果单击文件名,则通过AJAX调用下载该文件。 在我浏览目录之前,一切正常。 如果我导航到另一个目录或单击同一文件夹的侧边栏,文件列表会更新,但下载不再有效。 如何更新文件列表? 我执行ajax调用并刷新目录结构。 我从新目录中读取文件名并运行以下Javascript代码: jQuery(document).ready( funct

我正在建立一个包含文件存储的wordpress网站。 布局如下: 左侧有一个侧栏,显示完整的目录结构。 在侧边栏旁边的右侧有一个文件列表,其中包含作为链接的文件名。 如果单击文件名,则通过AJAX调用下载该文件。 在我浏览目录之前,一切正常。 如果我导航到另一个目录或单击同一文件夹的侧边栏,文件列表会更新,但下载不再有效。 如何更新文件列表? 我执行ajax调用并刷新目录结构。 我从新目录中读取文件名并运行以下Javascript代码:

jQuery(document).ready( function() {
   jQuery(".change_dir").click( function(e) {
      e.preventDefault(); 
      file_path = jQuery(this).attr("data-file_path");
      nonce = jQuery(this).attr("data-nonce");
      sid = jQuery(this).attr("data-sid");

      jQuery.ajax({
         type : "get",
         dataType : "json",
         url : myAjax.ajaxurl,
         data : {action: "change_current_directory", file_path : file_path, nonce: nonce, _sid:sid},
         success: function(response) {
             console.log(response);
            if(response.type == "success") {
                var fileList = document.getElementById("file-list");
                var files = response.file_list;
                var links = response.link_list;
                var innerHTMLText="";
                for (var i=0;i<files.length;i++){
                    var tmp= files[i].split("/");
                    var filename = tmp[tmp.length-1];
                    innerHTMLText+= '<a class="download_file" data-nonce="' + response.nonce + '" data-file_path="' + files[i] + '" data-sid="' + response.sid + '" href="' + links[i] + '">'+filename+'</a><br>';
                }
                console.log(innerHTMLText);
                fileList.innerHTML=innerHTMLText;
            }
            else {
               alert("Change failed.");
            }
         },
         error: function(error) {
            alert("Error occured: " + error.status + " " + error.statusText);
         }
      });
   });
});
现在应该执行JS代码(第一个代码块)。
我感谢你的帮助,提前谢谢

“但它不会触发.js文件在用户端执行下载”-这部分代码在哪里?它可能会将一些事件处理程序绑定到现有元素,但在AJAX调用后用新的元素替换这些元素时,这些事件处理程序可能会丢失。将download属性添加到锚定标记innerHTML字符串。你不需要js。如果通过ajax调用的php文件中有单独的js文件,js需要自我执行谢谢你的快速回复@AtulKumar我非常确定我需要js,因为要下载的文件不存在于wordpress目录中。如果单击该链接,该文件将通过API从另一台服务器下载(这与某些数据保护问题有关)。如果您仍能找到一种不用js的方法,请告诉我,这将大大简化整个过程。@04FS我认为这就是问题所在,我现在必须找到一种方法来添加这些事件处理程序…我已经有了一个想法。我将告诉你成功/失败的情况。
function loadDirectory($url,$data_field,$session_id,$path){
    $phpCode="";
    
    $data_field['_sid']=$session_id;
    $data_field['folder_path']=$path;
    //result contains directory structure
    $result = json_decode(get_raw_curl_data ($url,$data_field),true);
    //echo var_dump($result);
    $check_success = $result["success"];
    if (!$check_success){
        return "Some Error";
    }
    
    $allFiles = $result["data"]["files"];
    $nonce = wp_create_nonce("some_secret_nonce");

    foreach ($allFiles as $filemeta){
        if (!$filemeta["isdir"]){
            //filter for files, display the filename
            //and link it to the code that shall be executed
            $tmpName = $filemeta["path"];
            $link = admin_url('admin-ajax.php?action=download&_sid='.$session_id.'&file_path='.$tmpName.'&nonce='.$nonce);
            $phpCode = $phpCode.'<a class="download_file" data-nonce="' . $nonce . '" data-file_path="' . $tmpName . '" data-sid="' . $session_id . '" href="' . $link . '">'.$filemeta["name"].'</a><br>';
        }
    }
    //echo var_dump($phpCode);
    unset($filemeta);
    return $phpCode;
}
function loadSidebar($url,$data_field,$session_id,$path){
    $phpCode="";
    
    $data_field['_sid']=$session_id;
    $data_field['folder_path']=$path;
    //result contains directory structure
    $result = json_decode(get_raw_curl_data ($url,$data_field),true);
    //echo var_dump($result);
    $check_success = $result["success"];
    if (!$check_success){
        return "Some Error";
    }
    $nonce = wp_create_nonce("another_secret_nonce");
    $allFiles = $result["data"]["files"];
    foreach ($allFiles as $filemeta){
        //echo var_dump($filemeta);
        //filter for directories, display the dir name
        //and link it to the code that shall be executed
        if ($filemeta["isdir"]){
            $tmpName = $filemeta["path"];
            $link = admin_url('admin-ajax.php?action=change_current_directory&_sid='.$session_id.'&file_path='.$tmpName.'&nonce='.$nonce);
            $dirlist = dirListFromCurrentDir($url,$data_field,$session_id,$path."/".$filemeta["name"]);
            if (count($dirlist)>0){
                $phpCode = $phpCode."<button class='dropdown-btn'>".$filemeta["name"]."<i class='fa fa-caret-down'></i></button>";
                $phpCode = $phpCode."<div class='dropdown-container'>";
                foreach ($dirlist as $dirname){
                    $phpCode = $phpCode."<a class='change_dir' data-nonce='" . $nonce . "' data-file_path='" . $tmpName . "/" . $dirname . "' data-sid='" . $session_id . "' href='" . $link . "'>".$dirname."</a>";                    
                }
                $phpCode = $phpCode."</div>";
            }else{
                $phpCode = $phpCode."<a class='change_dir' data-nonce='" . $nonce . "' data-file_path='" . $tmpName . "' data-sid='" . $session_id . "' href='" . $link . "'>".$filemeta["name"]."</a>";
            }
        }
    }
    unset($filemeta);
    return $phpCode;
}
function change_current_directory() {
    function get_raw_curl_data($url,$data_field)
    {//code not relevant}
   function loadDirectory($url,$data_field,$session_id,$path)
        {//code not relevant}
   if ( !wp_verify_nonce( $_REQUEST['nonce'], "another_secret_nonce")) {
      exit("Woof Woof Woof");
   }   
   
   // fetch sid and path
   $sid = $_REQUEST["_sid"];
   $file_path = $_REQUEST["file_path"];
   $file_name = end(explode("/",$file_path));
   //Download File
   $url_files = "https://apiurl";
   $listFiles_field = [//APIData];

   $allFiles = ladeDirectory($url_files,$listFiles_field,$sid,$file_path);

   $download_nonce = wp_create_nonce("some_secret_nonce");
   $result['type'] = "success";
   //list with all file names
   $result['file_list'] = $allFiles[0];
   //list with the generated admin_url() link for each file
   $result['link_list']= $allFiles[1];
   $result['nonce']=$download_nonce;
   $result['sid']=$sid;
   // Check if action was fired via Ajax call. If yes, JS code will be triggered, else the user is redirected to the post page
   //At this point, the page refreshes without an action
   if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
      
      $result = json_encode($result);
      echo $result;
   }
   else {
      header("Location: ".$_SERVER["HTTP_REFERER"]);
   }
   // don't forget to end your scripts with a die() function - very important
   die();