jquery post behaviour help
It's meant to call a Action Method called Logon. depending on the logic i want it to either redirect to a url, or set some error text.
its getting the right information as json data, but all its doing is displaying the json result in a new tab when i click the submit button.
what is wrong here?
$("#submit").click(function () {
$.post({
url: "Account/LogOn",
dataType: "json",
开发者_JS百科 success: function (data) {
if (data.redirect) {
// data.redirect contains the string URL to redirect to
window.location.href = data.redirect;
}
else {
// data.form contains the HTML for the replacement form
$("#error").replaceWith(data.error);
}
}
});
You need to cancel the default action ...
return false;
at the end of the click
handler.
$("#submit").click(function () {
$.post({
url: "Account/LogOn",
dataType: "json",
success: function (data) {
if (data.redirect) {
// data.redirect contains the string URL to redirect to
window.location.href = data.redirect;
}
else {
// data.form contains the HTML for the replacement form
$("#error").replaceWith(data.error);
}
}
});
return false;
});
Why are you doing this on client side and not the server side?
On Server side you can redirect or redisplay the logon form with errors.
return Redirect(returnUrl);
return View(model); //model contains the Model Errors. It can be added using ModelState.AddModelError(...);
精彩评论