点击一个链接后,会触发ajax require /material/materialPrintajax
,这将返回JSON格式数据,返回的JSON数据已经通过http://jsonlint.com/
验证,网站告诉我我的返回JSON是有效的。但是当我遇到时,
console.log(jsonResult);
始终在firebug控制台中返回null。
stackoverflow中的前一个解决方案并没有解决这个问题,正如我所说:
http://stackoverflow.com/questions/6465468/why-parsejson-returns-null
http://stackoverflow.com/questions/8575479/parsing-json-string-returns-null
依旧......
以下是我的代码:
$.ajax({
url : $.att.config.BASE_URL + "/material/materialPrintajax",
type : "POST",
dataType : "json",
data: {
"choosenCourse" : choosenCourse,
"choosenDate" : choosenDate,
"choosenElCode" : choosenElCode
},
//read json result & display print data in HTML
success : function(re){
console.log('good!!',re);
jsonResult = jQuery.parseJSON(re);
console.log(jsonResult);
},
error :function(re) {
//eg. if return data is not json format
console.log('error!!',re);
}
});
如果您需要,这是我返回的JSON(重新):
{"COOKING":{"A":{"0":{"CLASS_MENU_SYMBOL":"A","STAFF_ID":"3010120001","LAST_NAME_KANJI":"\u6817\u7530","FIRST_NAME_KANJI":"\u6607","STAFF_START_TIME":"09:00","STAFF_END_TIME":"10:00","COURSE_ID":"0","COURSE_NAME_JP":"\u6599\u7406","RESERVE_COUNT":1},"RESERVE_TOTAL":3,"1":{"CLASS_MENU_SYMBOL":"A","STAFF_ID":"3010120012","LAST_NAME_KANJI":"\u9648","FIRST_NAME_KANJI":"\u6167\u5a77","STAFF_START_TIME":"21:00","STAFF_END_TIME":"22:00","COURSE_ID":"0","COURSE_NAME_JP":"\u6599\u7406","RESERVE_COUNT":2}},"B":{"0":{"CLASS_MENU_SYMBOL":"B","STAFF_ID":"3010120001","LAST_NAME_KANJI":"\u6817\u7530","FIRST_NAME_KANJI":"\u6607","STAFF_START_TIME":"13:00","STAFF_END_TIME":"14:00","COURSE_ID":"0","COURSE_NAME_JP":"\u6599\u7406","RESERVE_COUNT":1},"RESERVE_TOTAL":1}},"BREAD":{"B7":{"0":{"CLASS_MENU_SYMBOL":"B7","STAFF_ID":"3010120010","LAST_NAME_KANJI":"\u738b","FIRST_NAME_KANJI":"\u5a77","STAFF_START_TIME":"15:00","STAFF_END_TIME":"17:00","COURSE_ID":"1","COURSE_NAME_JP":"\u9762\u5305","RESERVE_COUNT":1},"RESERVE_TOTAL":1},"B1":{"0":{"CLASS_MENU_SYMBOL":"B1","STAFF_ID":"3010120010","LAST_NAME_KANJI":"\u738b","FIRST_NAME_KANJI":"\u5a77","STAFF_START_TIME":"15:00","STAFF_END_TIME":"17:00","COURSE_ID":"1","COURSE_NAME_JP":"\u9762\u5305","RESERVE_COUNT":1},"RESERVE_TOTAL":1}},"CAKE":{"12":{"0":{"CLASS_MENU_SYMBOL":"12","STAFF_ID":"3010120012","LAST_NAME_KANJI":"\u9648","FIRST_NAME_KANJI":"\u6167\u5a77","STAFF_START_TIME":"09:00","STAFF_END_TIME":"11:00","COURSE_ID":"2","COURSE_NAME_JP":"\u86cb\u7cd5","RESERVE_COUNT":2},"RESERVE_TOTAL":2}}}
答案 0 :(得分:4)
jQuery.parseJSON
期望JSON字符串作为参数。你传递的不是一个字符串。它已经是一个对象了。 jQuery自动将服务器返回的JSON字符串解析为javascript对象,该对象是传递给success
方法的对象。您无需再次致电parseJSON
。您可以直接使用该对象。
success : function(re){
console.log(re.COOKING.A);
},
答案 1 :(得分:2)
jQuery在进行AJAX调用时会自动尝试解析返回的数据。
这意味着,您实际上是在尝试解析已经解析过的对象。
解决方案是不要试图解析它并直接使用它。
jQuery.ajax()
(dataType和Data Types部分解释了它。)答案 2 :(得分:1)
只需将dataType更改为false
$.ajax({
url : $.att.config.BASE_URL + "/material/materialPrintajax",
type : "POST",
dataType : false,
data: {
"choosenCourse" : choosenCourse,
"choosenDate" : choosenDate,
"choosenElCode" : choosenElCode
},
//read json result & display print data in HTML
success : function(re){
console.log('good!!',re);
jsonResult = jQuery.parseJSON(re);
console.log(jsonResult);
},
error :function(re) {
//eg. if return data is not json format
console.log('error!!',re);
}
});