开发者

Invalid JSON in ajax from php file

First the cod开发者_StackOverflowe.

php file:

    $data = DB::typeAlerte()->all();

    for($i=0;$i<sizeof($data);$i++)
        $rep[] = $data[$i]['id'];

    echo json_encode($rep);

javascript file :

myJ.ajax({
    type:   'POST',
    url:    '../lib/form/f_idTypeAlerte.php',
    datatype:'json',
    success: function(msg) {
      alert(msg)
      msg = myJ.parseJSON(msg);
      alert(msg[0])
    }       
});

myJ.parseJSON(msg) catch this error : Uncaught Invalid JSON: ["COURROIE","PNEUS ARRIERE","PNEUS AVANT","VIDANGE"] jquery-1.5.1.js:869 whereas the JSON is ok

Why i have this error ?


Try like this:

myJ.ajax({
    type: 'POST',
    url: '../lib/form/f_idTypeAlerte.php',
    dataType: 'json', // <-- notice that this should be dataType, not datattype
    success: function(msg) {
        alert(msg[0]);
    }
});

Also I would recommend you having your server script set a proper application/json Content-Type HTTP header.


You have a case error when writing dataType. Don't forget javascript is case sensitive.

Instead of this:

  datatype:'json'

Should be this

  dataType:'json' //T is uppercase
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜