ariezona
ariezona

Reputation: 176

Weird "Parsing JSON Request Failed" - jQuery

I try to get JSON data from my server. But it works for some data and not working for some else.

I try to catch the error and it said Parsing JSON request failed.

this is the code

var url1 = 'http://example.com/example/bookdetail.php?id='+e;
$.ajax({
    type: "GET",
    url: url1,
    dataType: "json",
    success: function(data){
        //do my stuff with the JSON data


    },error:function(x, e){
                  if (x.status === 0) {
                     alert('You are offline!!\n Please Check Your Network. ' + x.reponseText);
                  }
                  else if (x.status == 404) {
                     alert('Requested URL not found.');
                  } else if (x.status == 500) {
                     alert('Internel Server Error.');
                  } else if (e == 'parsererror') {
                     alert('Error.\nParsing JSON Request failed.');
                  } else if (e == 'timeout') {
                     alert('Request Time out.');
                  } else {
                     alert('Unknow Error.\n' + x.responseText);
                  }
          }

});

And this is the JSON format data

Parsing failed data

[{
    "id": "480",
    "title": "Cocokologi dalam Dunia Agama",
    "photo": "1374573181_cover.jpg",       
    "pdf": "http://example.com/example/1374573181.pdf",
    "desc": "Apakah teks-teks kitab suci sejalan atau cocok dengan pandangan-pandangan saintifik modern, seperti diklaim oleh banyak kaum agamawan pada masa kini? ..."
 }]

working JSON format

[{
  "id": "330",
  "title": "Demokrasi dan Kekecewaaan",
  "photo": "1374497593_cover.jpg",
  "pdf": "http://example.com/example/1374497593.pdf",
  "desc": "Buku ini bermula dari orasi ilmiah yang disampaikan Goenawan Mohamad (GM) dalam acara Nurcholish Madjid Memorial Lecture (NMML), yang berlang- sung di Aula Nurcholish Madjid, Universitas Paramadina, Jakarta, pada 23 Oktober 2008 lalu. Acara ini adalah acara tahunan Yayasan Wakaf Paramadina (YWP)—kali ini untuk kedua kali, setelah di tahun sebelumnya Dr. Komaruddin Hidayat menyampaikan orasi sejenis untuk pertama kali. Selain untuk mengenang sosok dan pemikiran Cak Nur, be- gitu biasa..."
  }]

This 2 types JSON is from same source but different result. Is there any false JSON format from the first JSON data?

STACK TRACE RESULT

This is from stack trace
x.extend.parseJSON (jquery-1.10.2.min.js:4)
On (jquery-1.10.2.min.js:6)
k (jquery-1.10.2.min.js:6)
x.ajaxTransport.send.r (jquery-1.10.2.min.js:6)
Paused on exception. "SystaxError"

JSON DATA

[{ "id" : "330", 
"title" : "Demokrasi dan Kekecewaaan", 
"photo" : "1374497593_cover.jpg", 
"pdf" : "http://example.com/books/1374497593.pdf", 
"desc" : "Buku ini bermula dari orasi ilmiah yang disampaikan Goenawan Mohamad (GM) dalam acara Nurcholish Madjid Memorial Lecture (NMML), yang berlang- sung di Aula Nurcholish Madjid, Universitas Paramadina, Jakarta, pada 23 Oktober 2008 lalu. Acara ini adalah acara tahunan Yayasan Wakaf Paramadina (YWP)—kali ini untuk kedua kali, setelah di tahun sebelumnya Dr. Komaruddin Hidayat menyampaikan orasi sejenis untuk pertama kali. Selain untuk mengenang sosok dan pemikiran Cak Nur, be- gitu biasa..." }

Upvotes: 0

Views: 3273

Answers (1)

Girish
Girish

Reputation: 12117

If you have defined dataType: "json" parameter and also using error callback method with AJAX, then AJAX would be check response header Content-type

You should set text/json header in server response..

PS: The second JSON is looks correct but sometime space or enter ♪ might be come with response these character will not visible in response but these can be create issue with callback function.

Upvotes: 2

Related Questions