0

When I try parsing my JSON I don't even know why I get this error:

Uncaught SyntaxError: Unexpected token < login.js:7
$.ajax.success login.js:7
l jquery2.0.js:4
c.fireWith jquery2.0.js:4
k jquery2.0.js:6
(anonymous function)

and my jquery script is here:

$(".input-submit").click(function(){
    $.ajax({
        url: "./index.php?action=login",
        type: "POST",
        data: {login:$("#login").val(),password:$("#password").val()},
        success: function(data) {
            data = JSON.parse(data);
            console.log(data);
        }
    });
});
musicnothing
  • 3,977
  • 24
  • 43
KeizerBridge
  • 2,707
  • 7
  • 24
  • 37
  • 12
    Are you *sure* your web service is returning you valid JSON? (Hint: It's returning HTML or XML). Also, why parse it yourself? Set the dataType setting to 'json" and let jQuery do it for you. If it fails, the `error` callback will be triggered with an error of `'parseerror'` – Brad M Jun 11 '13 at 21:54
  • `<` could very well be the beginning of an XML tag. – voithos Jun 11 '13 at 21:55
  • Your json is invalid, and given that the bad token is a `<`, you've probably got some html embedded in the string (e.g. server-side php warnings/errors). The data returned by the server cannot contain **ANYTHING** except the json text. No html headers. No plain text. No headers, no footers. **JUST** json. – Marc B Jun 11 '13 at 21:56
  • 3
    Hit F12 in your browser and inspect the network tab to see what is *really* being returned. – Niels Keurentjes Jun 11 '13 at 21:57
  • 1
    what is the result of `console.log(data)`? (try putting it before your JSON.parse() method so it prints before the error) – Assimilater Jun 11 '13 at 22:03
  • ok thx so I had that dataType: "json" but now nothing is doesn't returned anything when a echo json_encode($tab); in php ... whereas all my value are posted in ajax. I checked my network. – KeizerBridge Jun 11 '13 at 22:03
  • 1
    http://jsonlint.com/ is invaluable for checking the validity of JSON.. use it to verify what the webservice is returning to you! – msturdy Jun 11 '13 at 22:04
  • Please edit your question to show the PHP code that you're using - that's likely where the problem lies. – nnnnnn Jun 11 '13 at 22:07
  • when I console it doesnt returned anything. .. – KeizerBridge Jun 11 '13 at 22:07
  • 1
    For debugging this try `console.log(data)` _without_ setting `dataType:'json'` (because if you set the datatype and it isn't valid JSON then the error handler would be called (and you haven't provided one) instead of the success handler). – nnnnnn Jun 11 '13 at 22:08
  • ok nnnnnn, it returned me all the HTML code of my page when I do a console log without a datatype:json... – KeizerBridge Jun 11 '13 at 22:11
  • therein lies the problem, rewrite your php script to only output the json **or** use jquery selectors to grab a container for your json, ie `$(data).find('span#myJSONContainer').text()` – Assimilater Jun 11 '13 at 22:14
  • additionally, [consider using jQuery's $.parseJSON()](http://stackoverflow.com/questions/10362277/jquery-parsejson-vs-json-parse) – Assimilater Jun 11 '13 at 22:17
  • Ok and how can I get my value posted with the $.ajax in php ? – KeizerBridge Jun 11 '13 at 22:20

1 Answers1

0

This error shows up usually when your backend service (index.php?...) is returning an html page instead of json. Make sure that everything is ok with the server response and double check that you are making the correct ajax call.

I had this error a lot of times and 99% of the cases was some error on the server.

If you are sure that your server response is good, then use a json validator to check the response. Maybe a undesirable character is going unescaped.

ejoncas
  • 329
  • 2
  • 13