2011-12-26 2 views
0

JsonAction에서 간단한 true/false json 반환을 얻으려고하고 있지만 $ (게시) 콜백 함수를 실행하지 않고 html로 계속 반환합니다. 여기 (jasonlint.com에서 확인) 브라우저의 응답입니다JsonAction의 Json이 콜백 함수를 실행하지 않는 HTML로 반환합니다.

[HttpPost] 
public JsonResult RegisterPartial(RegisterModel model) 
{ 

    return Json(new { Success = false }, JsonRequestBehavior.AllowGet); 
} 

JQuery와 .cshtml

$(function() { 
     $('#RegisterPartial').live("submit", (function (e) { 
      e.preventDefault(); 

     $.post($(this).attr("action"), $(this).serialize(), function (retorno) { 
      if (retorno.Success) { 
       alert('success'); 
       window.location(gup("returnUrl")); 
      } 
     }, "json"); 

    })) 
}); 

에서 : 다음은 코드입니다

{"Success":false} 

나는 또한 시도 contentTypes "응용 프로그램/json "과 Encoding.UTF8하지만 응답은 항상 jquery 콜백 함수를 실행하는 대신 html 페이지입니다. 또한 $ .ajax() 시도했지만 여전히 동일한 응답.

도움을 주셔서 감사합니다.

답변

0

무엇이 #RegisterPartial인지 명확하지 않습니다. 그것은 형식입니까? 제출 이벤트를 구독하는 동안이어야합니다. 그래서이 양식은 다음과 같이한다 :이 경우

@Html.BeginForm("RegisterPartial", "SomeController", FormMethod.Post, new { id = "RegisterPartial" }) 
{ 
    ... some input elements 

    <button type="submit">OK</button> 
} 

인가? 당신의 도움, 대린에 대한

$(function() { 
    $('#RegisterPartial').live("submit", (function (e) { 
     e.preventDefault(); 

     $.post($(this).attr("action"), $(this).serialize(), function (retorno) { 
      if (retorno.Success) { 
       alert('success'); 
       window.location(gup("returnUrl")); 
      } 
     }, "json"); 
    }); // <-- You've had a trailing) here. 
    // Only one) is necessary whereas you had 2 
}); 
+0

감사 : 또한 당신은 .live() 익명 함수를 닫을 때 (당신이 ) 후행 한) 자바 스크립트 오류를 ​​갖고있는 것 같다. 코드를 수정하여 지적한 바를 확인하면 양식의 ID가 변경되어 jquery 코드가 트리거되지 않았 음을 알았습니다. 이제 괜찮아. – Marcos

관련 문제