4

I'm trying the following:

   [System.Web.Http.AcceptVerbs("PUT")]
   public HttpResponseMessage MakePost(PostDto post) {
        try {
            var response = Request.CreateResponse(HttpStatusCode.Redirect); // tried MOVED too
            response.Headers.Location = new Uri("google.com");
            return response;
        } catch (Exception e) {
            ErrorSignal.FromCurrentContext().Raise(e);
            return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
        }
    }

Which seems to be partially working - when this is called, I see the POST request in chrome debugger. Nothing appears in the Response tab, but then I see a GET request sent to the new URI, yet the page never changes, and my AJAX call throws an error:

var options = {
        url: postUrl,
        type: type,
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        }
    };
return $.ajax(options)
        .done(function (response) {
            // do stuff
        })
        .fail(function (response) {
            alert('error) // this gets hit - shouldn't the browser have redirected at this point?
        }).complete(function () {
            // stuff
        });
};

If I inspect response, I see a Status 200 "OK".... I'm so confused.

What am I doing wrong?

1
  • 2
    Your expectations for redirect are probably wrong... Redirect is just 302 response that browser will treat as "now go to this page before returning to that AJAX request". Since redirect is cross-domain result can't be read.... Commented Jan 2, 2014 at 23:17

1 Answer 1

7

This happens because the code issuing the AJAX request follows the redirect, not the browser. This will then fail because the AJAX request tries to access a different domain. If you want to redirect the browser, you should return some JSON result or a custom HTTP header, manually pick this up in your jQuery, and do the redirect there.

In your controller:

var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Headers.Add("FORCE_REDIRECT", "http://google.com");

Then add a success callback to your AJAX call:

success: function(data, textStatus, jqXHR) {
    if (jqXHR.getResponseHeader('FORCE_REDIRECT') !== null){
        window.location = jqXHR.getResponseHeader('FORCE_REDIRECT');
        return;
    }
}

In the past, I've wrapped the controller result up in a custom action result class for reuse.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Ant. Is there anything wrong with sticking the URL into the response directly, instead of using a custom header?
Nope - there's no convention for this that I'm aware of, so I went with a custom header as it seemed appropriate.
One can do send some other flag from server and then upon that can redirect to url on client side . whats wrong with it instead of settings the response header.
@irfanMunir Nothing's wrong with sending a flag in the body instead of the header; I just tend to do it in the header as I feel that redirect instructions are effectively "response metadata," so that's where it belongs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.