weixin_33704591 2017-03-28 18:10 采纳率: 0%
浏览 58

无法读取属性api Rest

I have a problem with this api.

This is in html

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

This is in 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();

I have the following error

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)

The idea is that of the result of json (total_sales) but I still do not understand why it gives me the error, if I call JsonpCallback (data.reports) if I use

JsonpCallback (data.sales)

Does not give any results

Can you help me find a solution, Thanks !!

You can see my code here

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

  • 写回答

1条回答 默认 最新

  • weixin_33681778 2017-03-28 18:38
    关注

    The JSON you have isn't an array and thus has no .length. You can remove the loop

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

    Further, it's sales and not reports

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

    On the other hand, json.totals is an array, so if you'd like to iterate over it, you would do it like this:

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改