简体   繁体   English

尝试遍历JSON数组时出错

[英]Error when trying to iterate through JSON array

I'm trying to iterate through a JSON array, but i'm getting an error: 我正在尝试遍历JSON数组,但出现错误:

TypeError: invalid 'in' operand a TypeError:无效的“ in”操作数

The code I am using is: 我使用的代码是:

/**
 * 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);
        }
    });
});

The value returned by the AJAX call is: 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"}

How can I fix? 我该如何解决?

The response you're getting from the server is in string format because you've told so in dataType: 'text', of $.ajax configuration options. 您从服务器获得的响应是字符串格式,因为您已经在$.ajax配置选项的dataType: 'text',告知了这一点。

To get the response in JSON format, use 要获取JSON格式的响应,请使用

dataType: 'json',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM