-2

i haven't found response on differents post about that's problem ...

i call a servcie who returns a json data, the code works fine on localhot but don't work online...

my success method :

if (data !== null) {
    var _summoners = jQuery.parseJSON(data);
    var keys = Object.keys(_summoners);
    for (var i = 0; i < keys.length; i++) {
        console.log(keys[i]);
        $("." + keys[i]).text(_summoners[keys[i]]);
    };
}

and the data returned :

{
    "45260330": "SharkMister",
    "42215171": "Nietpopov",
    "40247365": "emaki",
    "49678659": "FakeThePump",
    "42127891": "Nutty Trickster",
    "45397483": "Enter Name Here1",
    "26589510": "pedrocsi",
    "23324155": "Abdi385",
    "46217784": "ExpliciitA",
    "37018042": "GabrikageBR"
}

i can't find my mistake, any idea ?

NB : i set the dataType on my ajax call to 'json'

and my returns headers is:

Accept  application/json, text/javascript, */*; q=0.01

And the exact error is :

SyntaxError: JSON.parse: unexpected character 
var _summoners  = jQuery.parseJSON(data);

The returns type object is a string well formated...

Ema.H
  • 2,862
  • 3
  • 28
  • 41
  • 4
    Could it be that `data` is **already** an object (not a string containing JSON)? – Felix Kling Feb 12 '14 at 18:40
  • 2
    With those headers I'd guess that jQuery has already parsed the data for you and that `data` is an object, not a string. –  Feb 12 '14 at 18:41
  • 1
    What does `console.log(typeof data)` show? Unless it says `string`, you shouldn't be parsing it. – Barmar Feb 12 '14 at 18:45
  • @Felix i have a json object in my firebug console request but when i try to use data without convertion i have : TypeError: _summoners is not an object var keys = Object.keys(_summoners); – Ema.H Feb 12 '14 at 18:46
  • The *response* headers would be the relevant ones, not your `Accept` header. Please show those to us. – Bergi Feb 12 '14 at 18:46
  • @Barmar yes, good, it's a string type ! – Ema.H Feb 12 '14 at 18:47
  • @Ema.H: OK, if it's a string then please log it and post its exact value. The data you presented is valid JSON. – Bergi Feb 12 '14 at 18:51
  • {"45260330":"SharkMister","42215171":"Nietpopov","40247365":"emaki","49678659":"FakeThePump","42127891":"Nutty Trickster","45397483":"Enter Name Here1","26589510":"pedrocsi","23324155":"Abdi385","46217784":"ExpliciitA","37018042":"GabrikageBR"} but maybe some\r ?! – Ema.H Feb 12 '14 at 19:00
  • possible duplicate of [SyntaxError: JSON.parse: unexpected character](http://stackoverflow.com/questions/19824224/syntaxerror-json-parse-unexpected-character) – Qantas 94 Heavy Aug 31 '14 at 00:29

3 Answers3

1

jQuery automatically parses the data for you when it can infer the data type and you haven't indicated otherwise (via the dataType option). When data already is a JS object, calling JSON.parse on it will fail (stringifying the data to JSON.parse("[object Object]")). Try

var _summoners = data;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • with only data i have this error : TypeError: _summoners is not an object var keys = Object.keys(_summoners); – Ema.H Feb 12 '14 at 18:44
  • Can you show us the whole ajax call, please? What does `console.log(data)` give, what `console.log(typeof data)`? – Bergi Feb 12 '14 at 18:45
0

This fiddle may help: http://jsfiddle.net/uvp93/2/

Your data may be in JSON object form, rather than string form. jQuery.parseJSON() takes string form JSON and converts it to a respective JavaScript object.

Try this:

if (data !== null) {
    var _summoners = $.parseJSON(JSON.stringify(data));
    var keys = Object.keys(_summoners);
    for (var i = 0; i < keys.length; i++) {
        console.log(keys[i]);
        $("." + keys[i]).text(_summoners[keys[i]]);
    };
}

If your data is in object form, just perform a simple assignment: _summoners = data

turnt
  • 3,235
  • 5
  • 23
  • 39
0

Ok, so i ve found a solution with EVAL ! I post it for someone with the same problem :

if (data !== null) {
    var _summoners = eval("("+data+")"); // $.parseJSON(data);
    var keys = Object.keys(_summoners);
    for (var i = 0; i < keys.length; i++) {
        console.log(keys[i]);
        $("." + keys[i]).text(_summoners[keys[i]]);
    };
}
Ema.H
  • 2,862
  • 3
  • 28
  • 41