2013-09-24 2 views
0

JQuery와 코드 체크 배열이 null인지

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

응답이 []으로 점점 벨로우즈 코드에서 그리고 그것은 if 문을 입력하지 않습니다.

빈 배열을 확인할 수있는 방법은 없습니다.

+0

'if (response.length == 0) '의 문제점은 무엇입니까? – letiagoalves

+0

alert (경고)를 시도하면 어떻게됩니까? ? – caramba

+0

http://stackoverflow.com/questions/6003884/how-to-check-null-values-in-javascript – Alvaro

답변

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") 
    } 
} 
+0

같은 결과가 나오지 않습니다. – Psl

0

나는 당신의 API에 의해 반환 값이 배열, 어쩌면 객체 나 문자열 아니라고 생각한다. 이미 jQuery 내가 jQuery.isEmptyObject()

는 의견에서이

jQuery.isEmptyObject(response); 

같은 시도를 사용하는 것을 선호합니다 사용하기 때문에 당신이 언급 한 이후이,

console.log(typeof response); 

를 사용

확인 문자열,이 방법은 다른 방법이 없습니다.

if (response === "[]") //Better 
if (response.length === 2) //Not good, yet can be used 
관련 문제