【问题标题】:Failed to load resource: the server responded with a status of 404 Not Found加载资源失败:服务器响应状态为 404 Not Found
【发布时间】:2018-06-08 10:39:41
【问题描述】:

我是 Wordpress 和 Ajax 的新手,并不真正知道如何解决这个问题,因此我们将不胜感激。

我正在开发一个 Wordpress 主题,其中数据库中的所有学生都显示在一个表格中:

    <?php

get_header();

if(have_posts()):
    while(have_posts()) : the_post(); ?>

<article class="post page">
<a href="<?php the_permalink(); ?>"><?php the_content(); ?>

<!DOCTYPE html>
<html>
<head> 



        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  

<style>

}
    img {
    vertical-align: middle; 

}
    table {
    border-collapse: collapse;
    width: 100%;
    border-spacing: 50px 0;
    text-align: center;
}


    .hover:hover{
        background-color: antiquewhite;

    }
    td{
        padding: 10px;
    }
    .aligncenter{
        text-align: center;
    }


    div.scroll {
    position: static;
    width: 700px;
    height: 400px;
    overflow: scroll;
    overflow-x: hidden;
    }
    a {
    text-decoration: none;
    }


</style>
</head>

<body>
    <div class="scroll"><a href="<?php the_permalink(); ?>"><?php the_content(); ?> </a>
<?php
$mysqli = NEW MySQLi('localhost','root','','wp_db');

        if(isset($_GET['order'])){
            $order = $_GET['order'];
        }else{
            $order = 'name';
        }

        if(isset($_GET['sort'])){
            $sort = $_GET['sort'];
        }else{
            $sort ='ASC';
        }

        $result = $mysqli->query("SELECT * FROM wp_absolventen ORDER BY $order $sort");

        $image = $row['bild'];

        if($result->num_rows > 0){

            $sort == 'DESC' ? $sort = 'ASC' : $sort ='DESC'; // short IF statements 
                 ?>
                 <div class="table-responsive" style=overflow-x:auto;>  
            <table class="table table-bordered">
            <?php
            echo "
            <th></th>
            <th class='aligncenter'>
            <a href='?order=name&&sort=$sort'>Name</a></th>
            <th class='aligncenter'><a href='?order=geburtsdatum&&sort=$sort'>Geburtsdatum</a></a></th>
            <th class='aligncenter'><a href='?order=stadt&&sort=$sort'>Wohnort</a></th>
            ";
            ?>
            <?php
            while($rows = $result->fetch_assoc())
            {
                $name = $rows['name'];
                $geburtsdatum= $rows['geburtsdatum'];
                $stadt = $rows['stadt'];
                $p='<img src="data:image/jpg;base64,' . base64_encode( $rows['bild'] ) . '"/>';
                $id = $rows['id'];

                ?>

                <tr class='hover'>


                <td> <?php echo $p; ?></td>



                <td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" /></td> 



                <td><?php echo $geburtsdatum; ?></td>



                <td><?php echo $stadt; ?></td>
                </tr>

               <?php

            }

        }else{
            echo "No records returned.";
        }
        ?> 
        </table>
         </div>


</body>
</html>
</article>
  <div id="dataModal" class="modal fade">  
      <div class="modal-dialog">  
           <div class="modal-content">  
                <div class="modal-header">  
                     <button type="button" class="close" data-dismiss="modal">&times;</button>  
                     <h4 class="modal-title">Employee Details</h4>  
                </div>  
                <div class="modal-body" id="employee_detail">  
                </div>  
                <div class="modal-footer">  
                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
                </div>  
           </div>  
      </div>  
 </div>  
    <script>  
 $(document).ready(function(){  
      $('.view_data').click(function(){  
           var employee_id = $(this).attr("id");  
           $.ajax({  
                url:"select.php",  
                method:"post",  
                data:{employee_id:employee_id},
                succes:function(data){  
                    $('#employee_detail').html(data);  
                     $('#dataModal').modal("show");  
                }  
           });  
      });  
 });  

 </script>










<?php 
    endwhile;

    else :
        echo '<p>No contect found</p>';
    endif;

get_footer();
?>

到目前为止一切顺利,但是当您单击学生姓名时,它应该会打开一个引导模式,其中会显示有关学生的信息(从数据库中获取)。

     <script>  
 $(document).ready(function(){  
      $('.view_data').click(function(){  
           var employee_id = $(this).attr("id");  
           $.ajax({  
                url:"select.php",  
                method:"post",  
                data:{employee_id:employee_id},
                succes:function(data){  
                    $('#employee_detail').html(data);  
                     $('#dataModal').modal("show");  
                }  
           });  
      });  
 });  

 </script>

这是调用另一个php文件(select.php),打开模态并从数据库中获取信息的函数。 select.php 文件:

<?php  
 if(isset($_POST["employee_id"]))  
 {  
      $output = '';  
      $connect = mysqli_connect("localhost", "root", "", "wp_db");  
      $query = "SELECT * FROM wp_absolventen WHERE id = '".$_POST["employee_id"]."'";  
      $result = mysqli_query($connect, $query);  
      $output .= '  
      <div class="table-responsive">  
           <table class="table table-bordered">';  
      while($row = mysqli_fetch_array($result))  
      {  
           $output .= '  
                <tr>  
                     <td width="30%"><label>Name</label></td>  
                     <td width="70%">'.$row["name"].'</td>  
                </tr>  

                ';  
      }  
      $output .= "</table></div>";  
      echo $output;  
 }  
 ?>

但是当我尝试点击学生姓名时,模式并没有打开,而是出现 404 错误:加载资源失败:服务器响应状态为 http://localhost/wordpress/sample-page/select.php 为 404(未找到)。两个文件都在同一个文件夹中。

代码不在 wordpress 文件夹中时有效。

MM

【问题讨论】:

    标签: php jquery ajax wordpress wordpress-theming


    【解决方案1】:

    将 php 文件保存为 page-select.php 并在 Wordpress 页面中创建一个名为“select”的空白页面就可以了!虽然不是 url: "/select" 我只使用了 "select" 并且它有效!再次感谢 Anthony 和 Thirumani guhan 的宝贵时间!:)

    【讨论】:

      【解决方案2】:

      尝试使用 data 属性发送文件路径,如下行

      <td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" /></td>
      

      可以

      <td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" data-file="<?php echo get_template_directory() . '/select.php'; ?>" /></td>
      


      在下一行,

      url:"select.php",
      

      可以

      url: $(this).data('file'),
      

      我还没有从我这边测试过。但这只是您的代码方式的一个想法。


      编辑:

      <td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" /></td>
      

      可以

      <td><input type="button" name="view" value="<?php echo $name; ?>" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs view_data" data-file="<?php echo get_template_directory_uri() . '/select.php'; ?>" /></td>
      


      在下一行,

      url:"select.php",
      

      可以

      url: $(this).data('file'),
      

      【讨论】:

      • 否则,你可以像这样使用url: &lt;?php echo get_template_directory() ; ?&gt;+ 'select.php',
      • 使用时:" url: $(this).data('file')," 错误:jquery.min.js:4 Failed to load file:///C:/xampp/ htdocs/wordpress/wp-content/themes/newthemeselect.php:跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https 出现!使用“url: + 'select.php'”时,错误:“Uncaught SyntaxError: Unexpected token:”出现
      • 将“select.php”替换为“/select.php”并将get_template_directory()替换为get_template_directory_url()
      【解决方案3】:

      我从另一个线程回答了类似的问题...基本上,建立/编写一个简单的php文件,如果您可以通过浏览器直接访问它,那么您可以通过ajax调用它...。设置是文件的前缀使用 page-"title-of-page".php 命名,然后使用类似于上述约定的 "title-of-page" 创建一个空白页面,然后设置您的永久链接 url。这是另一个thread

      【讨论】:

      • 我已将我的 select.php 更改为“page-select.php”,保存了我的 functions.php 所在的位置,创建了一个名为“select”的新空白页面,并在我的 ajax 中将此文件称为/选择。现在错误是 POST localhost/select 404 (Not Found) 顺便说一句:感谢您的快速回复:)
      • 转到您的设置 > 固定链接,应选择帖子名称。然后检查你的浏览器是否可以检索到 localhost/select,如果不能尝试 localhost/index.php/select
      • localhost/select:找不到对象,尝试了 localhost/index.php/select:XAMPP 站点打开(欢迎使用 xampp)
      • jquery.min.js:4 POST localhost/select 404 (Not Found),我是不是因为错误而对 jquery 做错了什么?
      • 你说的是:执行以下步骤: 1)创建一个 page-example.php 将这段代码放入其中:永久链接,然后选择“帖子名称”,保存您的更改。4)然后将其复制并粘贴到url框:thisinterestsme.com/example(如果您的.htaccess配置正确) thisinterestsme.com/index.php/example (如果您的 .htaccess 需要配置)无论哪种方式,您都应该看到带有文本的页面:“This is page-example.php”这有效
      猜你喜欢
      • 2021-07-22
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      相关资源
      最近更新 更多