开发者

ajax post redirect, jQuery, .NET MVC

I have a page "A" with a Form that submits an ajax POST to a .NET MVC action ("PostAction"). Depending on whether a user has been authenticated, it should either save the data and return to "A" page, or be redirected to a "Login" page, which upon Login, it should repost to "PostAction", save, and return to "A" page.

Here are the steps of the process:

If authenticated. This is currently working.

A -> PostAction -> A

If not authenticated. This doesn't work.

A -> PostAction -> Login (Log in via Facebook JavaScript SDK) -> PostAction -> A

I'm running into a problem marrying the ajax post with .NET MVC in the scenario a user is not authenticated. I'm not sure what's the best way to accomplish this, so any help would be appreciated.

Here's my ajax call on "A" page

        $.ajax({
            url: form.attr('action'),
            data: form.serialize(),
            type: (form.attr('method')),
            dataType: 'json',
            error: function (xhr, textStatus, exceptionThrown) {
                var errorData = $.parseJSON(xhr.responseText);
                var errorMessages = [];
                for (var key in errorData) {
                    errorMessages.push(errorData[key]);
                }
                //redirect to "Login" as a GET after parsing errorData???
            },
            success: function (data, textStatus, xhr) {
            }
        });

Here's my MVC code on PostAction

    [AcceptVerbs(HttpVerbs.Post)] 
        public ActionResult PostAction(FormCollection collection) {
            if (!Authorize()) {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                LoginError loginError = new LoginError();
                loginError.ReturnURL = "/";
                loginError.TargetURL = "/Bracket/Vote/";
                //save FormCollection to loginError????
                return Json(loginError);
            }
            //...save to DB
            return Json("Success");
    }
  1. Is there a better way to accomplish this?

  2. If the flow is correct, how can I redirect to "Login" but still retain all the data within the FormCollection? What about saving FormCollection into a session and retrieving it later?

  3. The Login action currently accepts a GET. Should I submit the e开发者_Go百科rror redirection as a GET, and ask the Login to submit a post to PostAction? This seems excessive since the login now needs to be customized to handle this specific request. I'm not sure if using Facebook to sign in would affect this as well.


1. Is there a better way to accomplish this?

In my opinion - yes. I'm confused as to why you're performing an AJAX login if you're going to redirect anyway. I would (and have) used a regular form. I also use Facebook connect as a single sign on provider with Forms Authentication. There are too many variables with an AJAX login (cookies, user who was authorized to view a page is no longer authorized, etc). Use a regular form, post to your action - which should be decorates with [Authorize] and therefore Forms Authentication will handle the ReturnUrl logic for you.

2. If the flow is correct, how can I redirect to "Login" but still retain all the data within the FormCollection? What about saving FormCollection into a session and retrieving it later?

Yes - I've had this problem as well. What I ended up doing was using a special action filter which was smart enough to see that the user isn't authenticated, grab the incoming POST data, stick it into tempdata and redirect to the login page. The corresponding action filter would then see the model in temp data and populate it in the ViewData when coming back from the login page. I can share this with you if you like but it's pretty trivial. Overall the answer is yes - you need some kind of session/tempdata storage.

Couple of other notes:

  1. What is "LoginError"?
  2. Why are you using FormCollection in the POST action? If you're on ASP.NET MVC 3, it has built in support for JSON model binding, so you can accept a regular POCO and as long as the JSON object matches the POCO properties, the model binding will populate the model. If you're not on ASP.NET MVC 3, there is a adapter than can help you do this.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜