SyntaxError: Unexpected Token <

SyntaxError: Unexpected Token <

I hope there is no problem asking. I am modifying a previously working project. I upgraded it from mysql to mysqli and testing it on Ubuntu instead of Windows. 

That being said, my ajax functions don't seem to work, although my json data is in proper format. According to the error callback, the content type is text/html

I have tried the following:
  • changing contentType in ajax call to "application/json"
  • viewed the response text, which was returning the correct thing.
  • changing the content type in the php header function but that didn't help
I have read on stackoverflow that sometimes the response comes wrapped in <html> tags...but I couldn't confirm that in my script.

  1. <script>
  2.     //alert("test");
  3.     $(document).ready(function(){
  4.         loadResults();
  5.         function loadResults(){
  6.             //alert('<?php echo urlencode($q)?>');
  7.             $.ajax({    
  8.                 url: 'fetch/getsearch.php?q=<?php echo urlencode($q)?>', 
  9.                 dataType: 'json',  success: function(data)        
  10.                 {
  11.                     //console.log(data);
  12.                     $('#users-results').html("");
  13.                     $('#tags-results').html("");
  14.                     var users = data[0].users;
  15.                     var tags = data[0].tags;
  16.                     
  17.                     if(!users[0].none){
  18.                         for (var i = 0; i<users.length; i++){
  19.                             $('#users-results').append("<a href='#' class='users' id='u_"+i+"'>"+users[i].user+"</a><br>");
  20.                         } 
  21.                     }else{
  22.                         $('#users-results').append("<span class='users'>"+users[0].none+"</span>");
  23.                     }
  24.                     
  25.                     if(!tags[0].none){
  26.                         for (var i = 0; i<tags.length; i++){
  27.                             $('#tags-results').append
  28.                             ("<a href='room.php?cat="+tags[i].cat+"&tag="+tags[i].tag.substr(1,tags[i].tag.length)+"' class='tags' id='t_"+i+"'>"+tags[i].tag+"</a><span>"+tags[i].cat+"</span><br>");
  29.                         } 
  30.                     }else{
  31.                         $('#tags-results').append("<span class='tags'>"+tags[0].none+"</span>");
  32.                     }
  33.                     
  34.                     $(".users, .tags").css('padding-left','15px');
  35.                     $(".tags").css('padding-right','15px');
  36.                     $(".users, .tags").css('font-size','16px');
  37.                     
  38.                 },
  39.                 error: function (xmlHttpRequest, textStatus, errorThrown) {
  40.                      console.log(xmlHttpRequest.getAllResponseHeaders());
  41.                      alert(errorThrown);
  42.                 }
  43.               
  44.             });         
  45.         }
  46. </script>
And my php:

  1. <?php

  2. require_once '../config/db.php';
  3. session_start();

  4. if (!isset($_GET['q']) || trim($_GET['q']) == "")
  5.     header('Location: ../alert/error.php');
  6. else {
  7.     $q = strtolower(urldecode($_GET['q']));
  8.     $q = str_replace("'", "''", $q);
  9. }

  10. $results = array();
  11. $users_array = array();
  12. $tags_array = array();

  13. $user_query = mysqli_query($con, "SELECT * FROM user_table WHERE `username` LIKE '%" . $q . "%' ORDER BY username");
  14. $tag_query = mysqli_query($con, "SELECT * FROM tag_table WHERE `tagname` LIKE '%" . $q . "%' ORDER BY tagname");
  15. if (mysqli_num_rows($user_query) > 0) {
  16.     while ($user = mysqli_fetch_array($user_query, MYSQLI_BOTH)) {
  17.         $users_array[] = array('user' => $user['username']);
  18.     }
  19. } else {
  20.     $users_array[] = array("none" => "No users were found.");
  21. }
  22. if (mysqli_num_rows($tag_query) > 0) {
  23.     while ($tag = mysqli_fetch_array($tag_query, MYSQLI_BOTH)) {
  24.         $tags_array[] = array('tag' => $tag['tagname'],
  25.             'cat' => $tag['category']);
  26.     }
  27. } else {
  28.     $tags_array[] = array("none" => "No tags were found.");
  29. }

  30. $results[] = array('users' => $users_array,
  31.     'tags' => $tags_array);
  32. // header("content-type:application/json");
  33. echo json_encode($results);
  34. mysqli_close($con);
  35. ?>
If anyone can tell me how to tackle this issue, it would be perfect.

Thanks.