Ich habe ein Problem mit einem Ajax Formular in MVC asp.net. Wenn ich das Formular posten und versuche, ein JSON-Ergebnis von der Aktion zurückzugeben, löst meine Methode Onsuccess
nicht aus und die Ansicht wird nicht angezeigt. Es gibt nur JSON-Objekte, die angezeigt werden, als ob jquery.unobtrusive-ajax.js
nicht funktioniert.MVC asp.net Ajax Form Aktion zurückgeben Json Ergebnis
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.form.js" type="text/javascript"></script>
<script type="text/javascript">
function OnSuccess(response) {
alert("oki");
if (response.success == true) {
$(location).attr('href', response.ReturnUrl);
}
else {
$('form').clearForm();
$("#message").css("display", "block");
}
}
</script>
<title>Login</title>
</head>
<body>
@using (Ajax.BeginForm("login2",new {ReturnUrl=Request.QueryString["ReturnUrl"]}, new AjaxOptions
{
OnSuccess = "OnSuccess"
}))
{
@Html.Label("Nom :", new { @class = "monlabel" })
@Html.TextBoxFor(m => m.Login, new { placeHolder = "Nom", @class = "tb" })
@Html.ValidationMessageFor(m => m.Login)
@Html.Label("Mot de passe :", new { @class = "monlabel" })
@Html.TextBoxFor(m => m.Password, new { placeHolder = "Mot de passe", @class = "tb" })
@Html.ValidationMessageFor(m => m.Password)
<input id="bouton" type="submit" value="Valider" /><br />
}
<div><p style="display:none" id="message">Authentification erronée</p></div>
</body>
</html>
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult login()
{
return View();
}
public JsonResult login2(Models.Utilisateur utilisateur, string ReturnUrl)
{
Models.LoginDataContext MyDb = new Models.LoginDataContext();
Models.Utilisateur tmpUser = null;
ViewBag.result = "oki";
tmpUser = MyDb.ValidateUser(utilisateur.Login, utilisateur.Password).FirstOrDefault();
if (tmpUser != null)
{
Session["User"] = tmpUser;
FormsAuthentication.SetAuthCookie(tmpUser.Login, false);
//return RedirectToAction("AccesValider");
if (ReturnUrl != null) return Json(new {success=true,@ReturnUrl=ReturnUrl},JsonRequestBehavior.AllowGet);
else return Json(new {success=true,@ReturnUrl=FormsAuthentication.DefaultUrl },JsonRequestBehavior.AllowGet);
}
return Json(new {success=false,@ReturnUrl =""},JsonRequestBehavior.AllowGet);
}
}
ich dieses Ergebnis:
{"success":true,"ReturnUrl":"/Home/Index"}
im Browser
Ihre 'jquery.unobtrusive-ajax.js' Laden ist nicht zu schaffen. Weitere Informationen finden Sie im Browser console.log. und der Dateipfad dieser Datei. Wenn irgendwelche Fehler bitte erwähnen Sie danke. –
Oder vielleicht 'UnobtrusiveJavaScriptEnabled' ist in Ihrer Konfigurationsdatei nicht auf' true' gesetzt. – Jacob