Why res.redirect doesn't work? - javascript

I make an http.post request from Angular to an api from backend.After post i make a redirect to another page.Redirect doesn't work..I want to make redirect from backend.Does anyone another method to redirect?

How do you expect a redirect to work in the context of an ajax request??
The server is responding a 3XX response in the context of your ajax request, so your application has the burden of how to handle that response.
Your application can handle the redirect by assigning window.location if a redirect is applicable.

Related

Flask redirect not happening in browser when trigger by JS callback function

I have a flask app with a button's onClick callback that is handled by javascript. The JS function calls an endpoint of the flask app (after collecting all the necessary data for the request from DOM elements) - it's not tied to a form. Within the flask endpoint, it redirects to another page. Looking in network tab, the request to flask_endpoint does not return a response because of the redirection. Response of request says Failed to load response data: No content available because the request was redirected. However, in the network tab, a separate request of the redirected route is created and the template is in the response of this request. But, it does not redirect the actual browser route to that response. Can someone explain why?
Javascript:
$('#add-button').click(callback)
async function callback(e) {
await axios.post(`/some_flask_endpoint`, {
...request_body...
});
}
Flask endpoint:
#app.route('/some_flask_endpoint', methods=["GET", "POST"])
def save_something():
# some logic...
return redirect('redirect_to_some_url')
It is obvious that you are requesting on an endpoint "/some_flask_endpoint" but you redirect the request to 'redirect_to_some_url'.
JS you show does not appear to redirect the browser to the new endpoint.
Back to your "questions",
if you are questioning the Fail to load, it just tells you that the endpoint does not have any actual responses because it is a redirect.
If you are questioning about nothing happened in the browser after the redirect, it is because the js has responsibility on how to handle the response. If you do not redirect the browser, the browser stays.

CORS issue while trying to redirect from POST method to frontend page with GET request

I would like to redirect from POST method to frontend page with HTTP status code 303.
Expected result is that browser after making POST request redirects to page specified in Location header.
Currently I am getting CORS failed error message and browser does not redirect to frontend page.
A redirect does not mean "Load this URL in the browser window". It means "You can get whatever you asked for here".
When you make an Ajax request using JavaScript, the response is provided to JavaScript.
If the response is a redirect, then the browser follows it automatically and provides the response to the redirect to JavaScript.
The URL you redirect to needs permission from CORS in order for the JavaScript to read the response.
Do not attempt to mix web services and regular page navigation
If you want to submit some data and load a new page: Use a form submission.
If you want to submit some data and handle the response with JS: Use Ajax.

Is there a way to get and parse the response from a redirection request?

Say I send a request to www.a.com and it returns 302 and redirect to location www.a.com/net/test.do,since it is an API, I would like to parse the response from this request, any way to do this?
Actually, what you can't do (in the browser) is get the 302 response. The second one is the only one you'd get. That behaviour is on purpose, as stated in the Fetch API spec:
Redirects (a response whose status or internal response’s (if any) status is a redirect status) are not exposed to APIs. Exposing redirects might leak information not otherwise available through a cross-site scripting attack.
In node.js, you can stop following redirects whenever you like.

Implement wrapper/global AJAX request handler?

In Angular.js Is it possible to implement a wrapper that will execute whenever a AJAX post request is made and gets a 401 HTTP response?
My web app makes dozens of RESTful requests to the server. If their login session expires the server returns a HTTP 401 response. Now, that doesn't trigger the 'fail' function of the post request (annoyingly). But even if it did, I don't want to now edit dozens of RESTful requests functions and have them check if the error response == 401 then refresh the page (to force the user to re-login).
I just want to implement a POST wrapper that will affect all current and future requests. Is this possible?

How to add request headers to window.open in javascript?

In my application I need to open a pdf in a new window. I'm making a window.open call for that. Mine is an MVC application. The url for the window.open contains my controller name and action method so that it will hit the respective action method.
But due to some reason, when I run with http request, I'm able to open the pdf, which means my controller call returns 200. But in the case of https, my controller throws a 302. Is there any solution for this?
One more thing which I found out is, when I compose the https request in Fiddler by adding some request headers to it, my controller returns 200. But I'm not able to add the request headers in window.open. Is there any way to do this?
I cannot go for a server side coding here. I have to complete it in the js itself.

Categories