簡體   English   中英

Json編碼的PHP數組附加了null

[英]Json Encoded PHP array has null appended to it

在調試的這一點上,這就是我要發送給我的javascript的內容

  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”。

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());
}

從您的php頁面返回一些內容,之后添加die()以刪除null

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM