尝试迭代JSON数组时出错

时间:2016-01-26 11:32:04

标签: jquery json

我正在尝试迭代JSON数组,但是我收到了一个错误:

  

TypeError:'在'操作数a'中无效'

我使用的代码是:

/**
 * Change periods if year is changed.
 *
 */
$('#year').on('change', function(e) {
    // Get year
    var year = $(this).val();

    // Send year to Laravel and get back json
    $.ajax({
        type: 'post',
        dataType: 'text',
        url: '/getWeekPeriods',
        data: {year: year},
        success: function (options) {
            // Remove existing options
            $('#period').empty();

            $.each(options, function(index, element) {
                alert(index);
            });


                //$option = $('<option></option>').attr('value', index).text(option);
                //$('#period').append($option);
        }
    });
});

AJAX调用返回的值是:

{"52":"21 Dec 2015 - 27 Dec 2015","51":"14 Dec 2015 - 20 Dec 2015","50":"7 Dec 2015 - 13 Dec 2015","49":"30 Nov 2015 - 6 Dec 2015","48":"23 Nov 2015 - 29 Nov 2015","47":"16 Nov 2015 - 22 Nov 2015","46":"9 Nov 2015 - 15 Nov 2015","45":"2 Nov 2015 - 8 Nov 2015","44":"26 Oct 2015 - 1 Nov 2015","43":"19 Oct 2015 - 25 Oct 2015","42":"12 Oct 2015 - 18 Oct 2015","41":"5 Oct 2015 - 11 Oct 2015","40":"28 Sep 2015 - 4 Oct 2015","39":"21 Sep 2015 - 27 Sep 2015","38":"14 Sep 2015 - 20 Sep 2015","37":"7 Sep 2015 - 13 Sep 2015","36":"31 Aug 2015 - 6 Sep 2015","35":"24 Aug 2015 - 30 Aug 2015","34":"17 Aug 2015 - 23 Aug 2015","33":"10 Aug 2015 - 16 Aug 2015","32":"3 Aug 2015 - 9 Aug 2015","31":"27 Jul 2015 - 2 Aug 2015","30":"20 Jul 2015 - 26 Jul 2015","29":"13 Jul 2015 - 19 Jul 2015","28":"6 Jul 2015 - 12 Jul 2015","27":"29 Jun 2015 - 5 Jul 2015","26":"22 Jun 2015 - 28 Jun 2015","25":"15 Jun 2015 - 21 Jun 2015","24":"8 Jun 2015 - 14 Jun 2015","23":"1 Jun 2015 - 7 Jun 2015","22":"25 May 2015 - 31 May 2015","21":"18 May 2015 - 24 May 2015","20":"11 May 2015 - 17 May 2015","19":"4 May 2015 - 10 May 2015","18":"27 Apr 2015 - 3 May 2015","17":"20 Apr 2015 - 26 Apr 2015","16":"13 Apr 2015 - 19 Apr 2015","15":"6 Apr 2015 - 12 Apr 2015","14":"30 Mar 2015 - 5 Apr 2015","13":"23 Mar 2015 - 29 Mar 2015","12":"16 Mar 2015 - 22 Mar 2015","11":"9 Mar 2015 - 15 Mar 2015","10":"2 Mar 2015 - 8 Mar 2015","09":"23 Feb 2015 - 1 Mar 2015","08":"16 Feb 2015 - 22 Feb 2015","07":"9 Feb 2015 - 15 Feb 2015","06":"2 Feb 2015 - 8 Feb 2015","05":"26 Jan 2015 - 1 Feb 2015","04":"19 Jan 2015 - 25 Jan 2015","03":"12 Jan 2015 - 18 Jan 2015","02":"5 Jan 2015 - 11 Jan 2015","01":"29 Dec 2014 - 4 Jan 2015"}

我该如何解决?

1 个答案:

答案 0 :(得分:2)

您从服务器获得的回复是字符串格式,因为您已在dataType: 'text', $.ajaxdataType: 'json', 配置选项中说明了这一点。

要以JSON格式获取响应,请使用

//item.js
[...]
let title = this.props.title;

someArrayWithObjs.map(function(item, key) {
  checkProcess(title);
})
相关问题