0

In the bellow code

 $.ajax({
     type: "post",
     url: "/signIn",
     dataType: "json",
     data: data,
     success: function (response) {
         alert("response----------------"+response) //[]
         if(response.length==0){
             alert("No data found")
         }
     }
  });

response is getting as [] and it doesn’t enter the if statement .

is there any possible way to check the empty array.

Donovan Charpin
  • 3,567
  • 22
  • 30
Psl
  • 3,830
  • 16
  • 46
  • 84

2 Answers2

0
$.ajax({
      type: "post",
      url: "/signIn",
      dataType: "json",
      data: data,
      success: function (response) {
        alert("response----------------"+response) //[]
        //modify if(response.length==0){
        if(!response || response.length==0){
            alert("No data found")
     }
}
sunysen
  • 2,265
  • 1
  • 12
  • 13
0

I suspect that the value returned by your API is not an array, maybe an object or string.

check this using

console.log(typeof response);

Since you already use jQuery I would prefer to use jQuery.isEmptyObject()

Try like this

jQuery.isEmptyObject(response);

From your comments, Since you mentioned string, then no way other way than this

if (response === "[]") //Better
if (response.length === 2) //Not good, yet can be used
Praveen
  • 55,303
  • 33
  • 133
  • 164