ajax调用后台方法返回页面显示为undefined 解决办法

时间:2021-04-30 19:37:44

二话不说先上代码:

$.ajax({

type: "GET",

url: "",

data: {},

async: true,

dataType:"json",

success: function(returnedData) {

var jsonObj = JSON.parse( JSON.stringify(returnedData) ); 

$("body").append('<h2>'JSON.parse(jsonObj.newslist[0]).content + '</h2>');

},

error: function(e) {

alert(e);

alert("error");

//请求失败时调用此函数

}

});

1、JSON.stringify(returnedData)的数据:

{"newslist":["{\"content\":\"2016.10.10开始做网站了呢\",\"id\":1,\"author\":\"zhao\",\"time\":\"2016-10-11 14:37:54.0\",\"title\":\"开始做网站\",\"type\":0,\"pictureurl\":\"http//:www.gto.com\"}","{\"content\":\"2016.10.10开始做网站了呢,大家很努力\",\"id\":2,\"author\":\"zhaoxin\",\"time\":\"2016-10-11 14:37:54.0\",\"title\":\"大家很努力\",\"type\":1,\"pictureurl\":\"http//:www.gto.com\"}","{\"content\":\"2016.10.10开始做网站了呢,大家很努力,杨晓辉\",\"id\":3,\"author\":\"yang\",\"time\":\"2016-10-11 14:37:54.0\",\"title\":\"一起加油\",\"type\":0,\"pictureurl\":\"http//:www.gto.com\"}","{\"content\":\"就是测试update功能\",\"id\":4,\"author\":\"哥特偶编辑\",\"time\":\"2016-10-13 11:30:51.0\",\"title\":\"今天是个好日子\",\"type\":6,\"pictureurl\":\"http/:www.GTO.com\"}","{\"content\":\"content\",\"id\":5,\"author\":\"author\",\"time\":\"2016-10-17 15:21:30.0\",\"title\":\"title\",\"type\":1,\"pictureurl\":\"pictureurl\"}","{\"content\":\"\\\"2\\\"\",\"id\":6,\"author\":\"\\\"1\\\"\",\"time\":\"2016-10-18 15:37:20.0\",\"title\":\"\\\"4\\\"\",\"type\":1,\"pictureurl\":\"\\\"5\\\"\"}","{\"content\":\"2\",\"id\":7,\"author\":\"1\",\"time\":\"2016-10-18 15:39:20.0\",\"title\":\"4\",\"type\":1,\"pictureurl\":\"5\"}"]}


2、jsonObj的数据:

[object Object]

3、jsonObj.newslist[0])的数据也是正常的啊:

{"content":"2016.10.10开始做网站了呢","id":1,"author":"zhao","time":"2016-10-11 14:37:54.0","title":"开始做网站","type":0,"pictureurl":"http//:www.gto.com"}


3、jsonObj.newslist[0]).content的数据:

显示时undefined 这就是问题所在了

4、只要一步就可以解决了

JSON.parse(jsonObj.newslist[0]).content