奇怪的“解析JSON请求失败" -jQuery的 [英] Weird "Parsing JSON Request Failed" - jQuery

查看:185
本文介绍了奇怪的“解析JSON请求失败" -jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从服务器获取JSON数据.但这适用于某些数据,不适用于其他数据.

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

我尝试捕获该错误,它说解析JSON请求失败.

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

这是代码

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);
                  }
          }

});

这是JSON格式数据

解析失败的数据

[{
    "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? ..."
 }]

可工作的JSON格式

[{
  "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..."
  }]

这两种类型的JSON来自同一来源,但结果不同.第一个JSON数据中是否存在任何错误的JSON格式?

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

堆栈跟踪结果

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数据

[{ "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..." }

推荐答案

如果您已定义dataType: "json"参数,并且还将error回调方法与AJAX一起使用,则AJAX将是检查响应标头Content-type

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

您应该在服务器响应中设置text/json标头.

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

PS:第二个JSON看起来正确,但有时spaceenter ♪可能会带有响应,但这些字符在响应中将不可见,但这些可能是回调函数造成的问题.

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.

这篇关于奇怪的“解析JSON请求失败" -jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆