Wordpress 中的自定义 Ajax 请求

Custom Ajax request in Wordpress

我在 header.php 中插入了这个脚本:

var loadData=function(){
   jQuery.ajax({
      type:"POST",
      url:"json_gallery_data.php"
   }).done(function(data){
      var videos=JSON.parse(data);
      for(var i in videos){
         alert("ok");
      }
   })
}

然后显示 file:json_gallery_data.php 未找到 我可以做什么? 谢谢!!!

我在这里做了一个很大的假设,即 json_gallery_data.php 存在于您的主题中(而不是在您的 WordPress 安装的根目录中)。因此,您需要将正确的 URI 传递给 AJAX 函数:

url: "<?php echo get_stylesheet_directory_uri() . '/json_gallery_data.php'; ?>"

详细了解 get_stylesheet_directory_uri() in the Codex