【问题标题】:Uncaught SyntaxError: Unexpected token < in JSON at position 0 from Codeigniter responseUncaught SyntaxError: Unexpected token < in JSON at position 0 from Codeigniter response
【发布时间】:2018-05-11 13:28:24
【问题描述】:
<option value="Petrol/Diesel">Petrol</option><option
value="Petrol/Diesel">Diesel</option>

是我在 chrome>network 中看到它时生成的输出。

不能使用'in'操作符来搜索'length' in

或者我得到上述错误。代码工作正常,直到&lt;script&gt; 中的成功,我从这里做错了什么?

$.each(JSON.parse(result), function() {            
  $('#fuel').append(result);
});  

型号:

public function getFuel($modelid, $variant)
{
  $query= "SELECT * FROM `pms_details` WHERE `model_id` = '".$modelid."' AND `Variant` LIKE '".$variant."' ORDER BY `pms_id` ASC";
  $result = $this->db->query($query);
  return $result->result();

  print_r($result->result());
}

查看:

<select id="fuel" onchange="getKm()" >
  <option value="na">Fuel</option>
</select>

<script>
  function getFuel() {  
    var getVariant = $('#variant').val();
    var car_model = $('#car_model').val();
    console.log("this is inside get Fuel");

    console.log(car_model);
    console.log(getVariant);

    $.ajax({
      url: "<?php echo base_url() ?>pms1/getFuel/"+car_model+"/"+getVariant, 
      dataType: "html",
      success: function(result) {
        $('#fuel').html('<option>SELECT</option>');
        $.each(JSON.parse(result), function() {            
          $('#fuel').append(result);
        });  
      }
    });
  }
</script>

控制器:

public function getFuel($modelid, $variant) 
{
    $car_model = $modelid;
    $getVariant = $variant;

    $this->load->model('M_address');
    $fuel = $this->M_address->getFuel($modelid,$variant);

    $fuel_list = array();
    foreach ($fuel as $fuels) 
    {
      array_push($fuel_list, $fuels->fuel);
    }
    $fuel_list = array_unique($fuel_list);
    // echo '<option value="na">Fuel</option>';
    foreach ($fuel_list as $fuel) 
    {   
      $ts = explode('/', $fuel);
      $ss = count($ts);
      foreach($ts as $tt)
      {
        echo '<option value="' . $fuel . '">' . $tt . '</option>';
        //I've tries adding json_encode on the above line.
      }
    }                      
  }

【问题讨论】:

  • Cannot use 'in' operator to search 'length' in... 或者我得到 Uncaught SyntaxError: Unexpected token 之后我做错了什么
  • result 是一个包含 HTML 的字符串。这就是为什么您无法将其解析为 JSON...
  • 删除此错误 - 未捕获的 TypeError: Cannot use 'in' operator to search 'length' in - 那应该怎么办?
  • 那么你为什么要这样做呢?响应是一个 HTML 字符串。首先不需要调用$.each 来生成该错误
  • 是的,我明白了。如果没有 $.each 行,我将无法获得输出。所以我更喜欢只使用json。感谢您的帮助

标签: jquery ajax codeigniter


【解决方案1】:

如果你想使用 json 那么你需要做如下的事情:

控制器:

$options = array();
foreach ($fuel_list as $fuel) 
{   
  $ts = explode('/', $fuel);
  $ss = count($ts);
  foreach($ts as $tt)
  {
    $options[] = array('value' => $fuel, 'name' => $tt);
    //echo '<option value="' . $fuel . '">' . $tt . '</option>';
    //I've tries adding json_encode on the above line.
  }
}
echo json_encode($options);     

JS:

$.ajax({
  url: "<?php echo base_url() ?>pms1/getFuel/"+car_model+"/"+getVariant, 
  dataType: "json",
  success: function(result) {
    $('#fuel').html('<option>SELECT</option>');
    $.each(result, function (index, data) {
         var new_option = new Option(data.name, data.value);
         $('#fuel').append(new_option);
    });
  }
});

虽然您也可以放弃 $.each 并像这样附加结果(使用您的方法):

success: function(result) {
        $('#fuel').html('<option>SELECT</option>');
        //$.each(JSON.parse(result), function() {            
          $('#fuel').append(result);
        //});  
      }

【讨论】:

  • 您的解决方案运行良好。谢谢。不幸的是,删除 $.each 行并没有给我输出。还有一个,我该怎么做 echo json_encode(array_unique($options)); ??错误:

    严重性:通知

    消息:数组到字符串的转换

    再次感谢您的帮助
  • 你不必在你的代码中这样做......并且数组唯一性不适用于多维数组。
  • 声望低于 15 人的投票会被记录下来,但不会公开显示。我得到这个消息。也许你的投票数已经增加了:)
  • 奇怪,通常你只会在不高于某个特定代表的情况下为别人的问题投票时才会看到。
  • IDK,它表达了同样的信息,我真诚地想为你的努力投票,很抱歉我目前还没有选择。
猜你喜欢
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 2021-03-02
  • 2019-08-14
  • 2020-10-10
相关资源
最近更新 更多