Redirect to a remote URL with a POST verb and form data in MVC3 - asp.net-mvc

I currently have a small HTML form for a third party payment service, which comprises a flashy submit button and some hidden fields. These hidden field values are specific to the payment service, but the I wish to display the submit button as just one of many payment service options on a view based on a base class payment model, so I don't want to have specifics in my base payment model. Ideally I would like the submit button to call into an action in a service specific controller, and this action will gather service specific data, and submit a post request to the service's remote site, just as if I had clicked the submit button on the html form for that service. How can I achieve this?

If you want the form to be posted from the user's browser, you're going to have to use some client-side script either on a timer or on the Ready() event.
Can you do it server-side using HttpWebRequest?

i doubt that we can post data when redirecting. However what you want to achieve may be done using WebClient object from System.Net namespace. Not sure if it solves your problem but you can instantiate and call it like
WebClient client = new WebClient();
client.UploadString("http://www.xyz.com", "post", "a=b&c=d");

Related

Which is the right way to call an external API when and Orbeon form is validated

I have created an Orbeon form with 5 fields:
First name
Second name
e-mail
Phone
Case description
I want to call an external REST API (Nuxeo: Updating a Document) when the form is validated.
Reading Orbeon documentation I don't find the right way to do this.
Based on the documentation found in Orbeon Http Services it looks like it doesn't provide a way to customize the JSON that I must send in the API call with the form fields values.
Here is the general idea:
You can setup what button you want to have at the bottom of your form (doc).
Each button, run a "process". A process is composed of a sequence of actions (doc).
One possible action is send(), which allows you to send the XML containing the data entered by users ("form data") to a service you provide (doc).
With this, you can setup your form to have a "Send" button, which upon activation runs validation, saves the data, and sends it to your service.

Open new window from GSP and forward to external URL

I need some help. I am converting a Struts application to Grails.
There is a particular action I am trying to convert.
There is a form in a jsp that asks a user to select a customer from a dropdown list and the user clicks one of two buttons.
When the user clicks on the "View Reports" button, an onclick event calls a javascript function that issues window.open on a STRUTS action class, passing the selected customer and selected action.
In the action class the http request has some attributes set (request.setAttribute (..)) and a forward is performed to an external application. The attributes that where set are used by the application for sign on. This is done as a POST.
My problem is I am not quite sure how to wire this flow using the Grails framework. I was able to get as far as the user selecting a customer, clicking an actionSubmit button, reading the selected customer from params, adding my attributes to 'request' and then..I am stuck.
How to open a new window? (Tried javascript way as was done with Struts).
Also I am able to issue a redirect to the external site in the controller, however a redirect is done as a GET and not a POST, as well as the redirect is done in the same window. Any help in laying this out would be awesome. Thanks
You can't redirect a user from server using POST.
I can see some possible solutions:
1 . If you don't need to pass through your server to validate or request some data, you can use this:
<form action="http://someotherserver.com" method="post">
2 . Create a controller that redirect to a page in your own site and in this page make a treatment that receives the paramters and then redirect the user to another domain using ajax.
In Grails, I've used createLink, with the 'base' attribute to do this.
<g:createLink base="${params.dynamicURL}">Link</g:createLink>
I think something like this would work, but you can research it here: http://grails.org/doc/latest/ref/Tags/createLink.html

Add Submit button on Orbeon Form Runner and then call custom Java REST API on submission

I am working on a POC (proof of concept) where I am evaluating Orbeon form builder/runner. I am using community edition, just in case, I will need to change the code, else the Professional Edition would do for POC.
I am integrating FormRunner with my web application running on different web server, which will call the FormRunner URL with some custom query parameters.
I want to remove the Summary, PDF and Review buttons from FormRunner and simply want the save button to function as submit button, so as clicking on it will submit the POST data to my custom Java code which will talk back to my web application, likely through REST API. The submit button then should redirect me to another page saying 'Your data has been submitted'.
I find it difficult to understand the area where I can change the code for expected behavior. Could you provide me some pointers in this regards please?
Thanks and Regards,
Mayuresh.
You shouldn't have to change the code for this, but instead use the oxf.fr.detail.buttons.*.* property to define which buttons are shown. Based on your description, you only need the workflow-send button, which, amongst other things can POST the form data to a service you specify.

Extra refresh after a submit

I know that whenever you submit a form, you will refresh a page. But is there a way of using javascript or whatsoever that allows the webpage to refresh one more time after a submit button is being input?
Thank you guys so much.
I am guessing that what you need to implement is the Post, Redirect, Get pattern, which is the recommended way in Grails applications. if you type grails generate-controller [somedomainclassname], you will see that that controller actually employs this pattern.
It works as follows:
Your form submits to a action inside a controller, let's call it save()
Your save() action then performs the necessary operations to actually save the object, when it is done, it sends the browser a 'redirect', much like this: redirect(action: "show", id: mySavedObject.id)
This will cause the browser to issue a GET for the url /mycontroller/show/{id}
That request is picked up by the show action of your controller. It then goes on to display the object that was just created.
This approach plays well with HTTP semantics and avoids annoying "do you want to repost your form data" questions for the end user.

What methods are available to stop multiple postbacks of a form in ASP.NET MVC?

A common web problem is where a user clicks the submit button of a form multiple times so the server processes the form more than once. This can also happen when a user hits the back button having submitted a form and so it gets processed again.
What is the best way of stopping this from happening in ASP.NET MVC?
Possibilities as I see it are:
Disable the button after submit - this gets round the multiple clicks but not the navigation
Have the receiving action redirect immediately - browsers seem to leave these redirects out of the history
Place a unique token in the session and on the form - if they match process the form - if not clear the form for a fresh submit
Are there more?
Are there some specific implementations of any of these?
I can see the third option being implemented as an ActionFilter with a HtmlHelper extension in a similar manner to the anti-forgery stuff.
Looking forward to hearing from you MVC'ers out there.
Often people overlook the most conventional way to handle this which is to use nonce keys.
You can use PRG as others have mentioned but the downside with PRG is that it doesn't solve the double-click problem, it requires an extra trip to the server for the redirect, and since the last step is a GET request you don't have direct access to the data that was just posted (though it could be passed as a query param or maintained on the server side).
I like the Javascript solution because it works most of the time.
Nonce keys however, work all the time. The nonce key is a random unique GUID generated by the server (also saved in the database) and embedded in the form. When the user POSTs the form, the nonce key also gets posted. As soon as a POST comes in to the server, the server verifies the nonce key exists in its database. If it does, the server deletes the key from the database and processes the form. Consequently if the user POSTs twice, the second POST won't be processed because the nonce key was deleted after processing the first POST.
The nonce key has an added advantage in that it brings additional security by preventing replay attacks (a man in the middle sniffs your HTTP request and then replays it to the server which treats it as a legitimate).
You should always return a redirect as the HTTP response to a POST. This will prevent the POST from occuring again when the user navigates back and forth with the Forward/Back buttons in the browser.
If you are worried about users double-clicking your submit buttons, just have a small script disable them immediately on submit.
You might want to look at the Post-Redirect-Get (PRG) pattern:
This really isn't MVC specific, but the pattern we follow on our web pages is that actions are performed with AJAX calls, rather than full page POSTs. So navigating to a url never performs an action, just displays the form. The AJAX call won't be in the history
Along with the disabling of buttons, you can add a transparent div over the entire web page so that clicking does nothing. We do this at my work and add a little friendly label saying processing request..
The most elegant solution I found was to use ActionFilters:
Blog post is here

Resources