简体   繁体   English

SyntaxError:位置4的JSON中的意外令牌<

[英]SyntaxError: Unexpected token < in JSON at position 4

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. SyntaxError:JSON中第4处JSON.parse()处JSON中的意外令牌<在JSON(VM1366 jquery.min.js:4)在ub(VM1366 jquery.min.js:4)在x(VM1366 jquery.min。 js:4)在XMLHttpRequest。 (VM1366 jquery.min.js:4) (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. 我通过ajax将数据传递到我的控制器以从数据库查询,然后将查询的数据作为json返回。

My ajax call: 我的ajax电话:

 $.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. 任何帮助将不胜感激。

$.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);
});

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

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