2015-12-22 20 views
3

我嘗試通過ajax訪問外部網站時遇到了一些問題。 404錯誤會在我嘗試訪問它時發生,因此無法存儲名稱參數數據並在我的數據表上打印。 這是錯誤提示之一ajax wordpress 404錯誤無法從外部網站檢索json並將其打印在我的數據表上

DataTables warning:table id = import - Ajax error。 有關此錯誤的更多信息。請參見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_product_010」},{ 「名稱」: 「_ testing_product_008」},{ 「名稱」: 「_ testing_product_007」 },{ 「名」: 「_ testing_product_006」},{ 「名」: 「_ testing_product_005」},{ 「名」: 「_ testing_product_004」},{ 「名」: 「_ testing_product_003」},{ 「名」: 「_ testing_product_002」 },{ 「名」: 「測試 產品槍」}]}

在我的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); ?> 

回答

0

請嘗試以下方法:

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

我不確定您是否需要基於您的JSON格式的所有其他選項。僅當您加載的數據表格式不兼容時,才需要dataSrc

+0

對不起,但它不工作。我仍然有同樣的錯誤。 – user1032181

+0

@ user1032181如果您在瀏覽器中打開開發人員工具,您在控制檯中遇到什麼錯誤? –