I have function which return json data
transformResponse : function(data) {
return JSON.parse(data).results;
}
When function call it getting error
Error: JSON.parse: unexpected character at line 2 column 1 of the JSON data
If their is no data then JSON.parse(data).results [ ] empty. I think the error is due to this [ ] object
So how to check is return empty is json or not??
test the return of json parse before you try to access to an element // edit, added test
var transformResponse = function(data){
if(( typeof data !== undefined) && ( typeof data !== 'undefined') && ( data !== null ) && (data !== "")){
var d = JSON.parse(data) ;
if(( typeof d !== undefined)&&(d!=null)){
return d.results;
}else{
return null;
}
}else{
return null;
}
};
transformResponse : function(data) {
var responseData = JSON.parse(data);
return (responseData && responseData.results) ? responseData.results:null;
}
if responseData does'nt have results or empty it will return null else results array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.