Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if JSON response data is empty [duplicate]

The JSON response looks like this when it is empty when viewed in the browser console:

{"data":{},"status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"http://url/form/BN217473","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"","xhrStatus":"complete"}

In angular script I am checking if the response is empty but the check is not catching the empty response as the logic block is never executed

if (response.data == '') {
    console.log("no data found");
}

How can I check that my response is not empty?

like image 296
rocket Avatar asked Aug 16 '18 10:08

rocket


1 Answers

Try below code snippet:-

if(!Object.keys(response.data).length){
     console.log("no data found");
 }

If you are getting data as empty object like data: {}, then you should check if there is any key inside the object or not.

like image 105
Sagar Kharche Avatar answered Oct 16 '22 16:10

Sagar Kharche