2014-10-13 4 views
1

jQuery에서 AJAX 양식 게시를하고 있습니다. 그러나 파이썬 서버에서 오류 코드를 반환하면 오류 대신 성공한 것으로 읽혀집니다. 내 코드는 아래와 같습니다. 나는 파이썬에서 멍청하다. 내가 명백한 실수를한다면 사과하십시오.python에서 jquery로 ajax 오류를 반환했습니다.

JQuery와 :

$.ajax({ 

       type:"post", 
       url: "/newform", 
       data:$('#my-form').serialize(), 
       success: function(msg){ 

        alert(msg); 


       }, 
       error: function(msg){ 

        alert(msg); 
       } 
      }); 

파이썬 코드 :

def post(self): 

    email = self.get_secure_cookie("user") 
    id_name = self.get_argument('id1', None) 

    try: 
     credentials = Credentials(email=email, id_name=id_name) 
     credentials.save() 

    except Exception, e: 
     print '%s' % str(e) 
     msg = 'Authorization Failed. Please check if the credentials are correct' 
     status = "error" 

    else: 
     msg = 'Connected' 
     status = "success" 

    print 'status: %s' % status 
    self.write({"status": status, "msg": msg}) 
+0

는 "성공"은 서버에서 오류가 서버 측에서 일어난 경우 자바 스크립트 아무 생각이 없습니다 (200)의 HTTP 상태 코드로 응답 것을 의미한다. – Brandon

+0

예, 멍청한 실수를하고있었습니다. 설명해 주셔서 감사드립니다. – ubh

+0

걱정할 필요가 없습니다. 다행스럽게 생각 했어. – Brandon

답변

3

코드에서 여전히 "상태": "오류"가있는 성공적인 HTTP 200 응답을 반환합니다.

그래서 자바 스크립트는 다음과 같이 수 :

$.ajax({ 
      type:"post", 
      url: "/newform", 
      data:$('#my-form').serialize(), 
      success: function(msg){ 
       if (msg.status == "success") { 
       //handle success here 
       } else { 
       //handle error 
       } 
      }, 
      error: function(msg){ 
      //handle server error, such as HTTP 404, 500 
      } 
     }); 
+0

설명해 주셔서 감사합니다. 이것은 효과가있다! – ubh

1

기술적 인 수준에서 더하는 오류가 없습니다. 기능 수준에서는 오류가 있습니다. 따라서 아약스 호출은 오류없이 완료됩니다. 성공 처리기에서 오류 또는 성공을 제공하는 상태 필드를 체크인해야합니다.

+1

오류를 지적 해 주셔서 감사합니다. – ubh

관련 문제