5

I have a web app I am working on:

$("#post").click(function () {

    var u = $('#u').val();
    var j = $('#j').val();

    $.post("http://www.myweb.php", {
            u: u,
            j: j
        })
        .done(function (data) {


            var obj = jQuery.parseJSON(data);
            alert(obj.status );
            //alert("Data Loaded: " + data);
        });

});

When it tries to retrieve the JSON I get:

Uncaught SyntaxError: Unexpected token o
Mike
  • 6,751
  • 23
  • 75
  • 132
  • So what's `data`...? – robertklep Jul 03 '15 at 16:58
  • 2
    There is no syntax error in the code that you posted, it must be elsewhere, perhaps in the JSON data. [From the jQuery docs](https://api.jquery.com/jquery.parsejson/): **Passing in a malformed JSON string results in a JavaScript exception being thrown** – rink.attendant.6 Jul 03 '15 at 16:58
  • @rink.attendant.6 the syntax error is coming from the JSON parser. – Pointy Jul 03 '15 at 17:01
  • see this:: http://stackoverflow.com/questions/18130081/what-is-causing-uncaught-syntaxerror-unexpected-token-o-with-parsejson-an – Sudhir Bastakoti Jul 03 '15 at 17:01

1 Answers1

13

You don't have to call .parseJSON(). Your response has already been parsed. You're getting that error because the object you pass to jQuery.parseJSON() is being converted to the string "[object Object]". The unexpected token is that "o" in "object".

Pointy
  • 405,095
  • 59
  • 585
  • 614