okay please help, i've been frustrated issue.
i need either set error value or perform redirect when user hits submit button. (its standard login form). need ajax.
i think working, when user clicks submit page displays: {"redirect":"/home/index"}
or {"error":"the user name or password provided incorrect."}
its not redirecting/displaying error how i'm intending to.
some background - form login form, , placed inside modal popup dialog.(jqueryui)
heres jquery:
$("#submit").click(function () { $.post({ url: "account/logon", datatype: "json", success: function (data) { if (data.redirect) { // data.redirect contains string url redirect window.location.href = data.redirect; } else { // data.form contains html replacement form $("#error").replacewith(data.error); } } }); return false; });
and heres action method:
[httppost] public jsonresult logon(logonmodel model, string returnurl) { if (modelstate.isvalid) { if (membershipservice.validateuser(model.username, model.password)) { formsservice.signin(model.username, model.rememberme); if (url.islocalurl(returnurl)) { return json(new { redirect = returnurl }); } else { return json(new { redirect = "/home/index" }); } } } return json(new { error = "the user name or password provided incorrect." }); }
heres form:
@using (html.beginform("logon", "account")) { <div> <fieldset> <legend>account information</legend> <div class="editor-label"> @html.labelfor(m => m.username) </div> <div class="editor-field"> @html.textboxfor(m => m.username) @html.validationmessagefor(m => m.username) </div> <div class="editor-label"> @html.labelfor(m => m.password) </div> <div class="editor-field"> @html.passwordfor(m => m.password) @html.validationmessagefor(m => m.password) </div> <div class="editor-label"> @html.checkboxfor(m => m.rememberme) @html.labelfor(m => m.rememberme) </div> <p> <input id="submit" type="submit" value="log on" /> </p> </fieldset> </div> }
ouch, whole $.post()-call wrong.
use this:
$("#submit").click(function (e) {e.preventdefault(); $.post( "account/logon", $(this.form).serialize(), function (data) { if (data.redirect) { // data.redirect contains string url redirect window.location.href = data.redirect; } else { // data.form contains html replacement form $("#error").replacewith(data.error); } }, 'json' ); return false; });
you've used notation of arguments expected in $.ajax() , in $.post() it's different.
Comments
Post a Comment