2011-12-26 10 views
0

JsonActionから単純なtrue/false jsonリターンを取得しようとしていますが、$(post)コールバック関数を呼び出さずに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()匿名関数を閉じたときにも、あなたは(あなたが末尾)を持っている)JavaScriptのエラーを持っているようです。あなたが指摘したことを確認するコードを改訂する際に、フォームのIDを変更してjqueryコードが起動されていないことがわかりました。今それはちょうどいいです。 – Marcos

関連する問題