1

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.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
Mageto Denis
  • 19
  • 1
  • 7
  • [Little Bobby](http://bobby-tables.com/) says [**your script is at risk for SQL Injection Attacks**](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). CodeIgniter has a ActiveRecord class to prevent this. – rollstuhlfahrer Mar 26 '18 at 19:52
  • 3
    Please check if your code actually returns valid JSON. There might be a HTML formatted error message in there. – rollstuhlfahrer Mar 26 '18 at 19:53
  • I can see the data that am posting but I am not getting a valid json as my response – Mageto Denis Mar 27 '18 at 15:52
  • Now, what did you get as your response? – rollstuhlfahrer Mar 27 '18 at 15:53
  • Nothing at all... Its null The status code is 302 The type is text/html and the initiator is my jquery.min.js file – Mageto Denis Mar 27 '18 at 16:30
  • Does your code go inside success or error methods ? Also, paste ci_site_url() output by doing console.log() – DpEN Mar 27 '18 at 17:59
  • @DpEN the code previously was not going inside the success but rather in the error method. Right now I solved it after realizing a had a logical error in my code. – Mageto Denis Mar 27 '18 at 22:07

1 Answers1

-1
$.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);
});
louk
  • 97
  • 6
  • $highschool is defined and $academiclevel has a value and the query result has a value. I am wondering why am not getting the json response in my success return – Mageto Denis Mar 27 '18 at 16:06
  • If you replace your code by the code above, what you get? Maybe the header of your request pose a problem – louk Mar 27 '18 at 17:22
  • While above code may answer the question but a bit of explanation would be wise thing to do. Like pointing out why you have removed quotes, headers, etc. You will likely get negative attention with just posting the code. – DpEN Mar 27 '18 at 17:55
  • @DpEN i'm not sure if the header pose the problem or not, for that i suggest him to try this code, if this code work so we can say that the problem is in the request header! – louk Mar 27 '18 at 18:10
  • @louk -- A **recommendation** without **knowing** you can fix the problem is a **comment** -- If you are *not sure* you're answer will fix the issue .. You shouldn't be answering the question, rather leaving a comment *suggesting* a fix in the OP .. – Zak Mar 27 '18 at 19:51
  • After many trials that proved futile, I realized it was a logical error brought about by a redirect in my construct. Thanks for the help guys I really appreciate. @louk The above code works perfectly fine. – Mageto Denis Mar 27 '18 at 22:06
  • @Zak Understand, when i t tried to leave comment the systeme told me that i don't have suffisant reputations (i started answering since 2 days), the reason why i set my response below as answer! – louk Mar 28 '18 at 12:44