【发布时间】:2017-08-21 23:03:54
【问题描述】:
我对这个 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 (data.sales)
不给出任何结果
你能帮我找到解决办法吗,谢谢!!
你可以在这里看到我的代码
【问题讨论】:
标签: javascript jquery json ajax woocommerce