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])
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?