Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser doesn't redirect

I have erlang web application with cowboy and angular.js.

I started my application, opened browser in '/' and got main.html with <input> and <button>. When i pressed to the button i send http POST request to the server to the /join/username with angular $http, got this POST request at the server and send redirect to the browser:

{ok, Req4} = cowboy_req:reply(303, [{"Location", <<"/chat.html">>}], <<"">>, Req3)
{:ok, req4, State}

Redirect response headers:

 HTTP/1.1 303 See Other
 connection: keep-alive
 server: Cowboy
 date: Thu, 19 Sep 2013 14:36:01 GMT
 content-length: 0
 Location: /chat.html
 Cache-Control: no-store

After sending redirect, i got "GET" request from browser with "/chat.html" path, it's ok. I read chat.html and send back to the browser, but browser doesn't render it and url doesn't change in browser. I see chat.html 200 in browser console.

Why browser doesn't render new page - chat.html?

UPD 1.

if i type /join/username in browser by hand it redirects normally. Why?

Thank you.

like image 659
0xAX Avatar asked Jul 27 '26 06:07

0xAX


1 Answers

You say you're making the call to the server using the $http service from angular. This means that you're making an AJAX call. The response data is sent to the success callback you supply when constructing the $http call. In this case, your response data will the payload of your chat.html file:

$http({method: 'POST', url: '/join/username'}).
  success(function(data, status, headers, config) {
    // data will be the content from chat.html
  });

The browser only visibly follows redirects when it is making requests as part of the normal navigation flow. AJAX requests do follow redirects, but the result is not shown in the browser window - it is returned to your code instead. This is why typing the address in the browser works for you.

If you want to follow that redirect you could simply have your HTML form submit directly /join/username and get rid of the Angular AJAX call.

like image 69
Rob Harrop Avatar answered Jul 30 '26 09:07

Rob Harrop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!