2013-04-02 2 views
0

바이올린 : http://jsfiddle.net/wy4wd/19/예기치 않은 결과

내가 JSON 개체를 구문 분석하고 있지만,이 때 ID not found을해야 Error 인 html로 초래되는 사람에 이르기까지 떨어지고 나는 이유를 알아낼 수 없습니다 .

success1 인 경우 제대로 작동합니다.

JSON은 게시물 요청에 의해 반환되지만 질문 용도로는 로컬로 선언하고 있습니다. 내가 제대로 이해하면

$(document).ready(function() { 
    var data = '{"success": "0","patient_cid": "0"}'; 
    var response = jQuery.parseJSON(data); 

    if (response.success == 0) { 
     if (response.patient_cid == 0) {    
      $('#resultpanel').html('<p>ID not found</p>'); 
     } 
     if (response.patient_ambassador == 0) {     
      $('#resultpanel').html('<p>ID found but not an ambassador</p>'); 
     }    
     if (response.soap_error == '1') {     
      $('#resultpanel').html('<p>SOAP error</p>').fadeIn('slow'); 
     }     
    } 
    if (response.success == 1){ 
     $('#resultpanel').html('<p>success</p>').fadeIn('slow'); 
    } 
    else {    
     $('#resultpanel').html('<p>Error</p>').fadeIn('slow'); 
    } 
}); 
+1

하나의 sidenote : 'success' 코드를 실제로 문자열로 래핑해야합니까? 그것은 단지 "성공"이어야하지 않습니까 : 0'? – jgauffin

답변

2

//... previous code here 
else if (response.success == 1){ 
//... the rest of the code here 

을해야합니다.

그렇지 않으면 첫 번째 오류 구문 분석이 실행되지만 마지막 else 문 코드로 바뀝니다.

+0

빌어 먹을, 나는 그것을 믿을 수 없다. 내 정신을 구 해주셔서 고마워. 너의 대답에 대해 – martincarlin87

1

JSON을 구문 분석하는 것과 아무런 관련이 없습니다.이 문제는 그 원인이되는 if 구문의 논리입니다.

실제로 패널은 잠시 동안 "ID를 찾을 수 없음"으로 설정되어 있지만 "오류"로 바뀝니다. 이것에 의해 오류에 리셋되고있는 다음 값이 당신이 원하는에 설정되는 한

if (response.success == 0) { 
    if (response.patient_cid == 0) {    
     $('#resultpanel').html('<p>ID not found</p>'); 
    } 
    else if (response.patient_ambassador == 0) {     
     $('#resultpanel').html('<p>ID found but not an ambassador</p>'); 
    }    
    else if (response.soap_error == '1') {     
     $('#resultpanel').html('<p>SOAP error</p>').fadeIn('slow'); 
    }     
    else {    
     $('#resultpanel').html('<p>Error</p>').fadeIn('slow'); 
    } 
} 
if (response.success == 1){ 
    $('#resultpanel').html('<p>success</p>').fadeIn('slow'); 
} 
+0

고마워. – martincarlin87

1

하지만 :

당신이 success == 0 먼저 처리하는 else을 넣어 및 조건의 체인을 만들기 위해 else if를 사용

0 : 나는 같은 것을 할 것 : 당신은 어떻게 거짓 Y와 진리-Y 작업 자바 스크립트에서 이해해야한다 $('#resultpanel').html('<p>Error</p>').fadeIn('slow');

라인

+1

''0 ''이 위증적이지 않습니다! JSON의 데이터는 숫자로 이루어져야합니다.'{ "success": 0, "patient_cid": 0}' – Lucero

+0

죄송합니다. –