簡體   English   中英

無法讀取屬性apiRest

[英]Cannot read property apiRest

我對此API有問題。

這是HTML

<div id="summary"></div>

這是在JS中

var consuKey = "ck_b04aa6f288ee9a5495dee9c5db0a6b136350e005";
var consuSecr = "cs_1f98d389d0f9b47cd3200023864cf9b7cba50574";

function callurl() {
$.ajax({
   url: 'https://test.juand.org/wc-api/v2/reports/sales?',
   data:{
        filter: {period: "last_week"},
        consumer_key: consuKey,
        consumer_secret: consuSecr
    },
type: "GET",
dataType: "json"
})
.done(function(data){
JsonpCallback(data.reports)
})
.fail(function(data){
console.log("no");
})
}

function JsonpCallback(json) {
for (var i = 0; i < json.length; i++) {
$('#summary').append('<b>Descripción:</b> ' + json[i].total_sales + '<br />');
$('#summary').append('<hr />');
}
}

callurl();

我有以下錯誤

Uncaught TypeError: Cannot read property 'length' of undefined
    at JsonpCallback (VM2284:68)
    at Object.<anonymous> (VM2284:60)
    at fire (VM2283 jquery-2.2.4.js:3187)
    at Object.fireWith [as resolveWith] (VM2283 jquery-2.2.4.js:3317)
    at done (VM2283 jquery-2.2.4.js:8757)
    at XMLHttpRequest.<anonymous> (VM2283 jquery-2.2.4.js:9123)

這個想法是json(total_sales)的結果,但是我仍然不明白為什么如果我使用JsonpCallback(data.reports)會給我錯誤,

JsonpCallback(數據。銷售)

沒有給出任何結果

您能幫我找到解決方案嗎,謝謝!

你可以在這里看到我的代碼

https://jsfiddle.net/JDLA1/a84v2x9w/2/

您擁有的JSON不是數組,因此沒有.length 您可以刪除循環

function JsonpCallback(json) {
  $('#summary').append('<b>Descripción:</b> ' + json.total_sales + '<br />');
  $('#summary').append('<hr />');
}

此外,它是sales而不是報告

.done(function(data){
    JsonpCallback(data.sales)
})

另一方面, json.totals是一個數組,因此,如果要遍歷它,可以這樣進行:

for (var key in json.totals) {
    console.log("key:", key, "value:", json.totals[key])
}

暫無
暫無

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

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