Submit POST data with no callback

144 views
Skip to first unread message

Matthew McLarty

unread,
Mar 21, 2018, 2:48:46 PM3/21/18
to GWT Users
I feel a little silly asking this since it feels like something that should be obvious.

How do I submit data to another URL, via POST (or GET if need be) without it being an ajax request? Do I need to construct a fake FormPanel and make it 1px big to submit the data to a URL? Can I use request builder and set the content-type header? The problem is that I need to browser to advance to that URL where the submission is processed but the GWT methods all seem to expect that to be an asynchronous event.

Please help?

Slava Pankov

unread,
Mar 21, 2018, 4:16:41 PM3/21/18
to GWT Users
You can have hidden (display: none) FormPanel with method="post". Then:
form.getElement().<FormElement> cast().setTarget("");
form.setAction(url);
form.submit();

That will redirect to specified url with POST.

Matthew McLarty

unread,
Mar 21, 2018, 4:19:39 PM3/21/18
to GWT Users
Darn, I was really hoping there'd be an cleaner way but if it works, it works, I guess. Thank you, Slava. :)

Matthew McLarty

unread,
Mar 22, 2018, 10:26:54 AM3/22/18
to GWT Users
Okay, for the record, this does not redirect the current page to the URL that is set to the action of the FormPanel. Any idea what I'm doing wrong?

                FormPanel form = new FormPanel();
                form
.setAction("https://www.FAKEDOMAIN.com/my-account/order-complete.html");
                form
.setHeight("1px");
                form
.setWidth("1px");
                form
.addStyleName(AppController.HIDDEN_CLASS);
               
               
Hidden transactionId = new Hidden();
                transactionId
.getElement().setAttribute("name", "transactionId");
                transactionId
.setValue(order.getOrder().getQuoteNumber());
               
               
Hidden transactionTotal = new Hidden();
                transactionTotal
.getElement().setAttribute("name", "transactionTotal");
                transactionTotal
.setValue(order.getTotalAmount().toString());
               
               
Hidden printURL = new Hidden();
                printURL
.getElement().setAttribute("name", "printURL");
                printURL
.setValue(order.getPrintURL());
               
               
FlowPanel formStuffer = new FlowPanel();
                formStuffer
.add(transactionId);
                formStuffer
.add(transactionTotal);
                formStuffer
.add(printURL);
               
                form
.add(formStuffer);
                view
.getPnlTrackingSubmitter().add(form);
                form
.submit();

Slava Pankov

unread,
Mar 23, 2018, 6:01:07 PM3/23/18
to GWT Users
For redirect you need: form.getElement().<FormElement> cast().setTarget("");

Matthew McLarty

unread,
Apr 4, 2018, 3:33:48 PM4/4/18
to GWT Users
Sorry it's taken me awhile to respond. I wanted to say thank you because this was the exact information I needed. Very much appreciated :)

Kirill Prazdnikov

unread,
Apr 11, 2018, 4:03:03 AM4/11/18
to GWT Users
What about JsInterop ?
Use the new fetch or old school XMLHttpRequest  

Matthew McLarty

unread,
Apr 11, 2018, 9:47:05 AM4/11/18
to GWT Users
The thing was that I actually needed to actually navigate to the target URL of the form post so a fetch/ajax/xmlhttprequest wouldn't have worked for me :\
Reply all
Reply to author
Forward
0 new messages