Phoenix
Phoenix

Reputation: 1075

jQuery.parseJSON always returns null in firebug console

After click a link, will trigger ajax require to /material/materialPrintajax, which will return JSON format data, and the returned JSON data has been verified through http://jsonlint.com/, the website tell me my return JSON is valid. But as I come across, console.log(jsonResult); always return null in firebug console.

Former solution in stackoverflow does not solve this, as I have refered:

http://stackoverflow.com/questions/6465468/why-parsejson-returns-null
http://stackoverflow.com/questions/8575479/parsing-json-string-returns-null

and so on..

Here are my codes:

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

});

in case you need, here is my returned JSON (re):

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

Upvotes: 0

Views: 1432

Answers (3)

Hayk
Hayk

Reputation: 11

just change dataType to 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);
}

});

Upvotes: 1

jQuery automatically tries to parse the returned data when doing a AJAX call.

This means, you're actually trying to parse an already parsed object.

The solution is to simply not try to parse it and use it directly.

See also

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039328

The jQuery.parseJSON expects a JSON string as argument. What you are passing is not a string. It's already an object. jQuery automatically parses the JSON string returned by the server into a javascript object which is what is passed to the success method. You don't need to call parseJSON once again. You could directly use the object.

success : function(re){
    console.log(re.COOKING.A);
},

Upvotes: 4

Related Questions