dqrm8199 2018-03-26 19:43
浏览 236

SyntaxError:位于第4位的JSON中的意外标记<

SyntaxError: Unexpected token < in JSON at position 4 at JSON.parse () at n.parseJSON (VM1366 jquery.min.js:4) at ub (VM1366 jquery.min.js:4) at x (VM1366 jquery.min.js:4) at XMLHttpRequest. (VM1366 jquery.min.js:4)

The above is the error I get. I have searched and read the answers online and none is working for me.

I have two pages(they both use the same controller function) that I use to calculate prices with, one page is working fine and the other is giving me this error. I am passing data via ajax to my controller for querying from the database, and I return the queried data as a json.

My ajax call:

$.ajax({
  headers: {
    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content'),
    "Accept": "application/json, text/plain, */*"
  },
  url: "<?php echo ci_site_url(); ?>project/calculate",
  type: "post",
  data: {
    "urgency": 3,
    "academiclevel": highschool,
    "words": 275
  },
  dataType: "json",
  success: function(data, textStatus, jqXHR) {

    var h = data[0][0].highschool;
    var m = data[0][0].master;
    var u = data[0][0].undergraduate;
    var d = data[0][0].doctoral;
    var w = data[1].words;
    var c = parseFloat(data[2].currency);
    if (h) {
      document.getElementById('amount').value = h * w * c;
    }
    if (m) {
      document.getElementById('amount').value = m * w * c;
    }
    if (u) {
      document.getElementById('amount').value = u * w * c;
    }
    if (d) {
      document.getElementById('amount').value = d * w * c;
    }
  },
  error: function(jqXHR, textStatus, errorThrown) {
    console.log(errorThrown);
  }
});

My controller code:

public function calculate(){
     $urgency = $this->input->post('urgency');
     $academiclevel = $this->input->post('academiclevel');
     $words['words'] = $this->input->post('words');
     $currency['currency'] = $this->input->post('currency');

     $sql = $this->db->query("SELECT `$academiclevel` FROM `custom_prices` WHERE `deadline` = '$urgency'");
     $data = $sql->result();

     $f = [$data,$words, $currency];
     echo json_encode($f); 
}

Any help will be appreciated.

</div>
  • 写回答

1条回答 默认 最新

  • douzhi2017 2018-03-26 20:08
    关注
    $.post("<?php echo ci_site_url(); ?>project/calculate",
    {
        urgency: 3,
        academiclevel: highschool
        words: "275"
    },
    function(results, status){
        var data = JSON.parse(results);
        console.log(status);
        console.log(data);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来