开发者

jquery post behaviour help

开发者 https://www.devze.com 2023-01-31 18:23 出处:网络
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.

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(...);
0

精彩评论

暂无评论...
验证码 换一张
取 消