2016-11-08 77 views
0

在此調試這一點就是我發送給我的javascriptJSON編碼PHP陣列已經空追加到它

header("Content-Type: application/json"); 
    //echo json_encode($full_product_list); 
    echo json_encode(array()); 

,這裏是我的Ajax調用每次

jQuery.get("non public api sorry ") 
    .done(function(data) { 
     console.log("I got a response") 
     console.log(data) 
    }) 
    .fail(function(data) { 
     console.log("Error?") 
     console.log(data); 
    }) 

它的錯誤並在數據爲空數組我的響應字符串是

「[]空」

整套樂趣被調用來引用相同的東西,我得到一個錯誤,並在我的JSON結尾處添加「null」。從你的PHP頁面

function getAllProducts() { 
    $full_product_list = array(); 
    $loop = new WP_Query(array('post_type' => 'product', 'posts_per_page' => -1)); 
    $pf = new WC_Product_Factory(); 

    while ($loop->have_posts()) : $loop->the_post(); 
    $post_id = get_the_ID(); 
    $product = $pf->get_product($post_id); 
    $attributes = $product->get_attributes(); 
    $attrs = array(); 

    foreach ($attributes as $attr) { 
     $name = str_replace("pa_fablab_", "", $attr["name"]); 
     $attrs[$name] = $attr["value"]; 
    } 

    $item = array(
     "id" => $post_id, 
     "name" => get_the_title(), 
     "price" => $product->get_price() 
    ); 

    if (sizeof($attrs) > 0) { 
     $full_product_list[] = array_merge($item, $attrs); 
    } 
    endwhile; wp_reset_query(); 
    header("Content-Type: application/json"); 
    //echo json_encode($full_product_list); 
    echo json_encode(array()); 
} 

回答

1

返回的東西,加上模()後刪除空

header("Content-Type: application/json"); 
    //echo json_encode($full_product_list); 
    echo json_encode(array("message"=>"ok")); 
    die(); 
+1

太謝謝你了!就是這樣。 –