Cannot Redirect After HTTP POST nodejs - html

I have been trying to do a HTTP POST of some input data from a HTML form to mongodb.
Although I am able to perform the database insertion of my data but unable to redirect to another HTML page after the operation is finished.
I have tried both res.render as well as res.redirect, but nothing seems to work.
However, I am getting a successful post /index 200 followed by a get /home 200.

On the front side maybe write something like this
$.post("/").then(function(data) {
window.location = data.redirectUrl;
});
And in the server side write this
res.send({err: 0, redirectUrl: "/"});

Related

Sequence of events for Fuzzy search on html page

I have a page html let's call it abc.html
There are AngularJS fields embedded in it.
I am now writing a GET and POST in scala which routes the fuzzy search arguments to the proper page on the server.
I am trying to understand the sequence in which things occur in order to implement a GET/POST requests (written in scala) which would happen when someone makes a search on the search bar on the abc.html page, and which would return elements from the database
Is it abc.html (search) --> http GET request --> backend ( AngularJS) --> Database?
In this case this would mean my http post or get request would pass in the html data model elements which would in turn hit the backend AngularJS controller page which in turn would hit the database, and the return ride would send the database results via an http request to the page?
Do I need to explicitly define my GET in terms of angular fields and the database model?
thanks
HTTP uses request-response pairs. This means you don't have to make another request to return anything to the client, you just need to write the proper response. Other than that, your idea is fundamentally right. The process would look something like this:
Type something into the search form on your HTML page
Submit the search form to your backend. This creates a GET or POST request depending on your form element's method attribute.
(At this point the browser is awaiting a response)
As the request reaches the server, your backend code can capture its data and make a query to your database.
(At this point the server is awaiting data from the database)
The database returns its results, your backend code is free to format it into a response to the client's original request.
The client receives the response and you can use your frontend code to display it to the user.

Is there a way to not show parameters in url

Is there a way to only show a clean url when doing a get request?
i.e. someone is send to a page:
http://domain.com/?param1=1&param2=2
And the user only sees :
http://domain.com
I tried it with a post-request but then you get these annoying pop-ups when someone refreshes the page or hits the back button.
Doing a post-redirect-get is also not possible since this increases the response time to much and the page is generated dynamically so it needs the parameters.
You could use URL rewriting when you are using Apache.
Or similar functionalities in other web servers.
There are 3 ways to pass parameters from a client to a server:
GET request; which you don't want to use
POST body (includes post-redirect-get); you don't wan to use POST
request header
The way to have a client pass arbitrary parameters in a request header is cookies.

What is the expected action after an HTML post call to the server?

Sorry if this seems like a silly question, but assuming one implements a HTML form with an action triggering a post call to a server, is the server supposed to return the 'next' page or can the post call be twisted to return only a JSON for processing by the same HTML page too?
Is there a standard defining what can or should be done after a HTML post call is performed or is everyone free to do what they want?
is the server supposed to return the 'next' page
In general, yes, although you can return a 204 No Content response.
can the post call be twisted to return only a JSON for processing by the same HTML page too?
It could return JSON, but the page couldn't process it because there is no standard way to process JSON.
If you want to deal with JSON, then you need to make the request with JavaScript instead of a regular form submission.
The browser will render the response returned by the server.
If you want to handle the response in code, use AJAX instead.

Using Express.js and NodeJS, Can you send JSON via redirect in the response body

I am trying to send JSON via a 302 redirect. Is that possible in ExpressJS. The API states that a body can be added the res.json(). For example:
res.json(302, {'name': 'larry'}).
On the receiving end (where the redirect goes too) the body is empty. Here is some sample code:
Sending App
app.get('/hello', function(req,res){
var data = {'name': 'larry'};
res.set('location', 'http://www.example.com/sending');
res.json(302, data);
});
Receiving App
app.get('/sending', function(req,res){
console.log('Body: ' + req.body)
res.send(req.body);
});
Note: the response headers for the 302 show a correct content-length of the data, but the receiving end shows an empty object.
Your code doesn't make sense. In the first route, you tell Express to respond with a 302 redirect and send some data. It is sending your JSON to the client, something you can see in the fact that you're getting a correct Content-Length.
However, your second route can't work. Firstly, GET requests cannot have a body; req.body will always be empty.
Secondly, it looks like you're assuming a client receiving a redirect will resubmit the entity-body of the redirect response in its request to the target (example.com/sending in this case). This is incorrect.
A spec-compliant HTTP client will never send a server's response as the body of another request.
HTTP clients traditionally treat a 302 like a 303, meaning that the request to the target URL is always a GET request, even if the original request was a POST. Thus, it is impossible for the redirected request to contain any kind of body.
If you want to send some kind of data to another server, you have two options:
Send the data on the query string with a 303 redirect.
// I'll use the built-in querystring module to convert an object into
// a properly URL-encoded query string.
res.redirect(303, '/url?' + querystring.stringify(data));
Respond 200 and use browser-side trickery to POST the data to the real destination.
res.send('<form id="redir" method="post" action="/url">' +
'<input type="hidden" name="foo" value="bar">' +
// ...
'</form>' +
'<script>document.getElementById("redir").submit()</script>');
Ugly, but works if this is intended for browsers. It won't work with requests made by servers or other non-browser tools. You might also want to include a "Continue" submit button for the luddites that insist on browsing with JS disabled.
I would only use this method if the amount of data you have will make the query string exceed the amount of characters practically allowable in a URL (~2048) or if the data payload contains sensitive content, which should generally not be included in the query string.

How to not follow form submit after sending data in Rails

I have a form in my view.
I only want to submit the data, without the rendering of any other template or following the post request to a new URL.
Are there any solutions to this besides structuring the whole thing about AJAX?
EDITED:
I have a multipart form..
part 1 submits data to a cache (using AJAX), then I use JQuery to hide that part of the form and show part 2. Part 2 is what submits the main data to a rails controller. That controller action is responsible for pulling the previously cached data and sending a POST request to an API that stores part 1 and part 2 in a database.
What I would like to do:
Have the rails app receive the form data and process the request, then I could use an AJAX request to append data to the page (clearing out the form, and showing the compiled data submitted if the post was successful, and an error message if not).
UPDATE:
I have tried the following and it does nothing when clicked:
$('#button').click(function(event){
event.preventDefault();
$(this).submit(); //also tried .send();
});
you could either redirect to the same page or maybe do this
$('#selector').click(function(event){
event.preventDefault();
//Do other thing, or maybe nothing
});
In your controller do the following:
respond_to do |format|
format.all { render nothing: true, status: 200 }
end