ajax wordpress获取404错误无法从外部网站检索json并将其打印在我的数据表上


ajax wordpress getting 404 error unable to retrieve json from external website and print it on my datatable

当我试图通过ajax访问外部网站时,遇到了一些问题404错误当我尝试访问它时会发生,因此无法存储名称参数数据并在数据表上打印。这是错误提示之一

DataTables警告:表id=import-Ajax错误。有关此错误的详细信息。请看http://datatables.net/tn/7

下面是我的代码,请帮忙,谢谢。

<?php echo datatable_scripts(); ?>
<script>
$(document).ready(function() {
    $('#import').DataTable({
        ajax: {
            url: 'http://example.com/hello.php',
            type: 'GET',
            dataSrc: 'Data'
        },
        columns: [
            {data: 'name'}
        ]
    });
});
</script>

下面是我从外部网站输出的json文件。

{"数据":[{"名称":"_testing_product_009"},{"姓名":"_testing_prouct_010"}、{"名字":"''testing_prauct_008"}产品喷枪"}]}

在我的hello.php代码上

<?php
header("Access-Control-Allow-Origin: *");  //To enable cross-domain
include("../../../wp-blog-header.php");  //To enable wordpress core function

    $args = array( 
        'post_type'   => 'product', 
        'post_status' => 'publish',
        'nopaging'    => true
    );
    $query = new WP_Query( $args ); // $query is the WP_Query Object
    $posts = $query->get_posts();   // $posts contains the post objects
    $output = array();
    foreach( $posts as $post ) {  
        $output['data'][] = array( "name"=>$post->post_title );
    }
    echo json_encode($output); ?>

请尝试以下操作:

$("#import").DataTable({
  "ajax": "http://example.com/hello.php",
   columns: [
     {data: "name"}
   ]
});

我不确定您是否需要基于JSON格式的所有其他选项。只有当您加载的JSON格式与DataTables不兼容时,才需要dataSrc