Page not getting rendered via Express when using post (NodeJS) - post

I am using Express v.2.4.6 (Node.js - v.0.6.2).
I am trying to render (or redirect to) a new page once POST is called (as opposed to GET). I am able to render/redirect when GET is called. However, I cannot seem to render a page when POST is called in Express. I am not sure if this is even possible though the guide on the Express site does mention an example where you can redirect once POST is called.
The relevant code is given below (client is sending the form in JSON). I can parse through the JSON message successfully in Node.j.
Sample route:
app.post('/signup', function(req, res){
res.redirect('index');
//res.render('index');
});
No exceptions are thrown, but the index page does not get rendered nor redirected.
Any feedback will be appreciated.

Maybe you call it from $.ajax from client-side
It works well if you call it from server-side
Please refer to this question.
Express.js Won't Render in Post Action

res.redirect('/') is most likely what you wanted there, but you can render in any route, redirecting is just a convention that most people use

Related

(Rust, Actix Web) How to change method when using actix_web::web::Redirect::to(url)?

I am trying to handle a POST request to the /upload path. Once everything is done, I want to redirect to the main page. However, when I do that, it uses the POST method (as shown in the picture). Is there a way to redirect using the GET method?
Redirect to main page uses POST method
Error Message:
POST http://0.0.0.0/ [HTTP/1.1 405 Method Not Allowed 0ms]
#[post("/upload")]
async fn upload(req_body: String) -> impl Responder {
// ...
// This redirects to homepage but uses POST. Is there a way to redirect to GET http://0.0.0.0/?
actix_web::web::Redirect::to("http://0.0.0.0/")
}
I've tried using the methods in the Redirect Crate but it still redirects with POST
I've tried implementing redirect directly from the http form from where I'm sending the POST request. This is what I tried
Redirects are "307 Temporary Redirect" by default. Those keep the method for accessing the next page. You want "303 See Other" by using the see_other method, which will convert all methods to GET.
actix_web::web::Redirect::to("http://0.0.0.0/").see_other()
This link from the Redirect documentation explains more.

render :js not working

I am working on window 7 and facing issue with render
render :js => "alert('Hello')".
But it is not getting alert.
Do any one have experience with this issue.
You should make the call using AJAX so you should add the param "remote: true" to the link or form that is making the call to that action
Be sure that there is client side requested with JS format, as in that cases Rails set up content type for js, and not text/html.
It seems like you are trying to debug your code. Using a before_filter is not the best way. A request can only render once.
I suggest adding statements to your logging, and using the javascript/development console of your browser (Chrome/Firefox): this will show what goes over the wire perfectly,a dn what is executed on the client, and why it fails (if it fails).
Hope this helps.

Rails take base64

In rails i need to take a base64 string, and use it to generate a picture in rails. Now i'm having trouble, because i need to interact with AJAX calls (im strictly working on the server side, another guy is doing that client work) to send pictures. So far i've been taking requests in my application by having data transferred through the url (in the AJAX requests) but now im not sure if it's possible to transfer such a huge string through the url. How could i take in the data (like how could he pass it to me) to generate a picture.
Note: i've been using paperclip for my application so far, but now uploading through the form is not an option, it needs to be in an AJAX call where data is passed in a single call.
You're right, most browsers limit the length of a URL. The limit on IE8/9 is 2083 characters. Even if your particular browser has a higher limit, many servers limit the URL length as well (apache's default limit is right around 8k). It would be best to submit the image as a POST request with the data in the POST body.
I would use jQuery to POST JSON data to the server. In the controller, if this is set up correctly, you won't have to do a thing to parse the JSON. ActiveSupport will recognize the content type and parse it out into the params hash automatically.
Actually posting the data will depend on which javascript library you're using. Here's an example in jQuery, which you'd probably want to wire up to the onclick event of a submit button. This assumes you have a named route called process_image. This code would go in your view.
$.post(<%= process_image_path %>, { b64_img: "your_base64_image_data" });
In your controller, you can access the posted data with params[:b64_img]. If you want to return something from the controller back to the client, you can do this in the controller:
render :json => #model_object
And change the jquery call to look like this so you can do something with the return value:
$.post(<%= process_image_path %>, { b64_img: "your_base64_image_data" },
function(data) {
// do something with the data returned by the controller
});
Hope this helps. You can read more about the jQuery post call I used here: http://api.jquery.com/jQuery.post/
Dan

Is it possible to RedirectToRoute over POST instead of GET?

I'm trying to perform a redirect from one controller to another while passing along some parameters. Unfortunately the value of the parameters are long and obnoxious looking in the GET query string. Is there anyway I can use POST instead on the redirect to get a nicer looking URL?
Thanks.
As everyone said, you cannot redirect to post.
However, you can avoid ugly URLs by sticking your values in TempData instead of the route.
You can't do a proper POST redirect, but you can use JavaScript in the browser to mimic a POST redirect:
<form id="myform" action="http://mywebsite.com/">
<input name="myparameter" value="12345" type="hidden" />
</form>
<script>
document.getElementById("myform").submit()
</script>
It's not a true redirect and it won't perform as well as a redirect, but this should work.
A "Redirect" is a GET. If you're doing a real direct what you're doing is essentially informing the browser to go to another url. The browser than makes another http call (using GET) to the new url. The url may contain parameters but it will always be a GET and not a POST.
What you could do is store some data in session and then when the second (redirected) request comes in, you can access these values from session.
Sorry, no. Redirects via POST are simply not supported in the HTTP spec. Most clients implementing the common kinds of redirects (301 - Permanent and 302 - Temporary) issue a GET request to the new location.

How to post a form with MVC controller?

How do I post a form to an external site using an ASP.NET MVC controller?
EDIT: Ok... I have a view with some data on it. After the user completes the form I need to do a postback with a specified form format to an external site (like for paypal). After I posted the constructed form I need to redirect the action to a new view
You have to do the POST on the server-side..
of which this guy has written a helper class to do Http Post in C# (pastebin-ed). Check it out.
Send the post with the PostSubmitter class and just render your view normally.
Basically, in situation like this one would create a HttpWebRequest, set Method to post and write the post data to the request stream. But the linked code already does that for you in a nice and cozy way.
So no need rewire anything.
You can just manually set the action in the form tag to wherever you want to post to...
edit -
That is to say that you should manually create the form tag..
Instead of:
<% using (Html.Form<Controller>("Action", c => c.Method())) { %>
You should use:
<form action="http://www.someotherwebsite.com/action">
How about sending a redirect to the browser.

Resources