title: Jquery ajax parsing error for json
body: <p>I am trying to send some data using php and jquery ajax using json datatype method.
  Here is my code:</p>
  
  <pre><code>$("#username").on("keyup change keypress", function () {
      var username = $("#username").val();
      $.ajax
      ({
          type: "POST", // method send from ajax to server
          url: window.location.protocol + '//' + window.location.host + '/' + "admin/admins/user_exists",
          data: {
              username: username
          },// Specifies data to be sent to the server
          cache: false, // A Boolean value indicating whether the browser should cache the requested pages. Default is true
          contentType: "application/json",
          dataType: 'json', // The data type expected of the server response.
          success: function (response_data_from_server) {
  
              for (var key in response_data_from_server)
                  var result = response_data_from_server[key] + ""; // JSON parser
  
              if (result == 'true') {
                  console.log("---------------- in true");
                  $("#username_alert").text("ERROR");
                  $('#username_alert').removeClass("alert-success");
                  $("#username_alert").css("visibility", "visible");
  
              }
              else {
                  if (result == 'false') {
                      console.log("---------------- in false");
                      $("#username_alert").text("NO ERROR");
                      $("#username_alert").css("visibility", "visible");
                      $('#username_alert').addClass("alert-success");
                  }
                  else {
                      if (result == 'empty') {
                          console.log("---------------- in empty");
                          $("#username_alert").text("ERROR");
                          $("#username_alert").css("visibility", "visible");
                          $('#username_alert').removeClass("alert-success");
                      }
                  }
              }
          },
          error: function (jqXHR, textStatus, errorThrown) {
              console.log(textStatus, errorThrown);
          }
      });
  
  });
  </code></pre>
  
  <p>and it always goes to an error function. The error that I receive is the following: </p>
  
  <blockquote>
    <p>parsererror SyntaxError: Unexpected token ﻿ {}</p>
  </blockquote>
  
  <p>My url location is correct and is indeed returning the correct json format. Here is my php code:</p>
  
  <pre><code>public function user_exists()
        {
            $username = $this-&gt;input-&gt;post("username");
            $is_exists = "false";
            $this-&gt;load-&gt;database();
            if ($username != "")
            {
                $rows = $this-&gt;db-&gt;query("
                                          SELECT * FROM `admins` WHERE `username` = '" . $username . "'
          ")-&gt;num_rows();
                if ($rows &gt; 0)
                {
                    $is_exists = "true";   
                }
                else 
                {
                    $is_exists = "false";
                }
            }
            else
            {
                $is_exists = "empty";
            }
            $arr = array ('result' =&gt; $is_exists );
            $response = json_encode($arr);
            echo $response;
  
        }
  </code></pre>
  
  <p>I've debugged it million times, the firebug sees the response as correct and expected json, however the client side seems to refuse to get it as a json respone, for what I believe.</p>
  
  <p>Will appreciate any help!</p>