函数.php当变量未硬编码时脚本中断

functions.php script breaks when variable isn't hard coded

本文关键字:编码 脚本 中断 php 变量 函数      更新时间:2023-09-26

我编写了一个jQuery脚本,该脚本采用使用此函数生成的JSON编码列表在我的主题函数中运行.php并为我的jPlayer创建一个播放列表。但是,该脚本仅在 $file 变量进行硬编码(例如 OH0400 )时才有效。但是我需要它根据正在加载的页面选取$file变量。但是当我切换到此方法(使用 URL)时,脚本显示 JSON 为空。

我以多种方式运行脚本,硬编码$file和基于变量$file之间的输出似乎是相同的。为什么在进行切换时会得到空?

这是我的主题函数中的 PHP,.php。

 function MyjPlayerList(){
    $url = explode( '/', $_SERVER['REQUEST_URI'] );
    $file = strtoupper($url[2]);
    //$file = 'OH0400';
    $filename = '/dir/oralhistory/mp3files/'.$file.'*.mp3';
    $FILES = glob( $filename );
          foreach( $FILES as $key => $mp3 )  {
          $mp3 = str_replace( '/dir/oralhistory/mp3files/', '',$mp3);           
          $FILE_LIST[ $key ][ 'title' ] = $mp3;
          $FILE_LIST[ $key ][ 'mp3' ] = 'http://websiteurl.org/mp3files/'.$mp3;   
    } 
    $myjplayerdata = json_encode( $FILE_LIST ); 
    header ( 'Content-type: application/json' );    
    echo $myjplayerdata;
    exit;
    die();
     };

这是我的javascript:

    ajax_player = function() {
        jQuery('div#player').load('/js/player.html' , function() {
            var cssSelector= {
                jPlayer: "#jquery_jplayer_1",
                cssSelectorAncestor: "#jp_container_1"
            };
        var playlist = [];
        var options = {
            swfPath: "/js/Jplayer.swf",
            supplied: "mp3",
            smoothPlayBar: true,
            keyEnabled: true
        };
        var myPlaylist = new jPlayerPlaylist(cssSelector, playlist, options);
         jQuery.ajax({
                  url: "/wp-admin/admin-ajax.php" ,
                  type: "POST",
                  dataType: "text json",
                  data: { action: "MyjPlayerList"},
                  success:(function(data) {
                jQuery.each(data, function(index, value){
                        myPlaylist.add(value); // add each element in data in myPlaylist
                      console.log(data);        
                })
                  })//function (data) close
                })//ajax close  
        })//jquery.load  
}//ajax_player

是的,请检查您使用的字符编码。这可能是问题所在。

多亏了 Marc 的调试,事实证明,当我在 page 中运行脚本时我得到的和当我调用带有 javascript 的脚本时得到的都是不同的。 它试图使用admin-ajax.php而不是URL。