3

I have met with this error telling me that there is an unexpected token C pointing to my Jquery file. After much research, i am under assuming that the reason why i am getting this error is because the Json value passed back is already decoded and thus decoding it again will result in this error.

Is this statement true ? or is there another reason behind ? This is my what my json data looks like [{"comments":"Greta"},{"comments":"John"}]

<a onclick="showUser('.$row['ID'].')" >Show Comments</a>

<script>
function showUser(str) {
      if (str=="") {
        document.getElementById("txtHint").innerHTML="";
        return;
      } 
      $.ajax({
          type:'post',
          url: 'viewCommentsJson.php',
          data:{q:str},
          success:function(data)
          {
              data = $.parseJSON(data);
              var response;
              $.each(data, function(index, value){
                   response += value+'<br />';
              });
              $('#txtHint').html(response);
          }
      });
}
</script>
2
  • 1
    Json value passed back is already decoded and thus decoding it again will result in this error. Yes its true See console error jsfiddle.net/satpalsingh/j65Ce
    – Satpal
    May 20, 2014 at 13:07
  • From the jQuery docs about .ajax() (in reference to the dataType parameter: "The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). "...
    – War10ck
    May 20, 2014 at 13:12

2 Answers 2

2

The reason is that, you are trying to parse the response which is already in json format.

$.parseJSON method should apply in string type. Since your server response is json, you dont have to parse it again.

Change your code like this,

$.ajax({
    type: 'post',
    url: 'viewCommentsJson.php',
    data: {
        q: str
    },
    success: function (data) {
        var response = "";
        $.each(data, function (index, value) {
            response += value.comments + '<br />';
        });
        $('#txtHint').html(response);
    }
});
4
  • so are you suggestion that i remove` data = $.parseJSON(data); `completely and leave the rest as per normal ? May 20, 2014 at 13:55
  • 1
    By doing that i will get this Uncaught TypeError: Cannot use 'in' operator to search for '74' in Connected to MySQL<br>[{"comments":"ashdhasasbsd"},{"comments":"sdds"}] May 20, 2014 at 14:32
  • there isnt a specific line. It is pointing to my jquery-1.9.0.js:945 May 21, 2014 at 0:01
  • Can you alert that data in succes. And see what is voming from server May 21, 2014 at 3:17
1

This is presumable because the response is 'Connection failed ...' and you are trying to parse the database output to JSON, when the connection was refused.

This would cause the C to be parsed and would throw an "Unexpected token C" error.

Check your 'network' tab in your inspector and look for the .PHP SQL script. At least in the chrome inspector you can get the response and see when kind of response you are getting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.