未捕获的TypeError:无法在'中使用'运营商搜索'长度'

时间:2018-02-02 04:25:20

标签: javascript jquery json ajax

我收到以下错误



Uncaught TypeError: Cannot use 'in' operator to search for 'length' in <!DOCTYPE html>

jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:570

  at isArrayLike (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:570)
    at Function.each (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:368)
    at Object.success (1:356)
    at fire (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:3233)
    at Object.fireWith [as resolveWith] (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:3363)
    at done (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:9841)
    at XMLHttpRequest.callback (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:10312)
&#13;
&#13;
&#13;

我的代码的.each部分似乎失败了:

&#13;
&#13;
        $.each(data, function(arrID, arrValue) {
          for(var d = resetUTC(arrValue.start_date); d<=resetUTC(arrValue.end_date); d.setDate(d.getDate()+1)){
            unavailableDates.push($.datepicker.formatDate('yy-m-d', d));
          }
&#13;
&#13;
&#13;

我尝试过很多东西,例如

&#13;
&#13;
$.each(JSON.parse(data), function(arrID, arrValue) {
&#13;
&#13;
&#13; 但最终得到了更多的错误:

&#13;
&#13;
Uncaught SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.success (1:356)
    at fire (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:3233)
    at Object.fireWith [as resolveWith] (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:3363)
    at done (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:9841)
    at XMLHttpRequest.callback (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:10312)
&#13;
&#13;
&#13;

超级迷失,会感激任何帮助。非常感谢提前!

以下完整代码:

&#13;
&#13;
<script>
  function checkDate(date) {
    ymd = date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate();
    return [$.inArray(ymd, unavailableDates)== -1];
  }
  function resetUTC(d){
    var f = moment.utc(d);
    var now = new Date(f._d);
    var utcDate = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
    return utcDate;
  }
  $(function(){
    unavailableDates = [];
    $.ajax({
      url: '<%= preload_facility_room_path(@room)%>',
      dataTyp: 'json',
      success: function(data) {
        $.each(data, function(arrID, arrValue) {
          for(var d = resetUTC(arrValue.start_date); d<=resetUTC(arrValue.end_date); d.setDate(d.getDate()+1)){
            unavailableDates.push($.datepicker.formatDate('yy-m-d', d));
          }
        });
        $('#reservation_start_date').datepicker({
          dateFormat: 'yy-mm-dd',
          minDate: 0,
          maxDate: '1y',
          beforeShowDay: checkDate,
          onSelect: function(selected){
            $('#reservation_end_date').datepicker("option", "minDate", selected);
            $('#reservation_end_date').attr("disabled", false);
            var start_date = $('#reservation_start_date').datepicker('getDate');
            var end_date = $('#reservation_end_date').datepicker('getDate');
            var nights = (end_date - start_date)/1000/60/60/24 + 1;
            var input = {
              'start_date': start_date,
              'end_date': end_date
            }
            $.ajax({
              url: '<%= preview_facility_room_path(@room)%>',
              data: input,
              success: function(data){
                if(data.conflict){
                  $('#message').text("These dates are not available.");
                  $('#preview').hide();
                  $('#btn_book').attr('disabled', true);
                }
                else{
                  $('#message').text("");
                  $('#preview').show();
                  $('#btn_book').attr('disabled', false);
                  var total = nights * <%= @room.price%>
                  $('#reservation_nights').text(nights);
                  $('#reservation_total').text(total);
                }
              }
            });
          }
        });
        $('#reservation_end_date').datepicker({
          dateFormat: 'yy-mm-dd',
          minDate: 0,
          maxDate: '1y',
          beforeShowDay: checkDate,
          onSelect: function(selected){
            $('#reservation_start_date').datepicker("option", "maxDate", selected);
            var start_date = $('#reservation_start_date').datepicker('getDate');
            var end_date = $('#reservation_end_date').datepicker('getDate');
            var nights = (end_date - start_date)/1000/60/60/24 + 1;
            var input = {
              'start_date': start_date,
              'end_date': end_date
            }
            $.ajax({
              url: '<%= preview_facility_room_path(@room)%>',
              data: input,
              success: function(data){
                if(data.conflict){
                  $('#message').text("These dates are not available.");
                  $('#preview').hide();
                  $('#btn_book').attr('disabled', true);
                }
                else{
                  $('#message').text("");
                  $('#preview').show();
                  $('#btn_book').attr('disabled', false);
                  var total = nights * <%= @room.price%>
                  $('#reservation_nights').text(nights);
                  $('#reservation_total').text(total);
                }
              }
            });
          }
        });
      }
    });
  });
</script>
&#13;
&#13;
&#13;

0 个答案:

没有答案