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.