XMLHttpRequest is an alternative for HTTP calls from GWT client side and allows the control over all aspects of requests/responses. But how to use it? javadoc address: http://www.gwtproject.org/javadoc/latest/com/google/gwt/xhr/client/class-use/XMLHttpRequest.html
How to use XMLHttpRequest in GWT?
2k Views Asked by hadiafifi At
1
There are 1 best solutions below
Related Questions in JAVA
- I need the BIRT.war that is compatible with Java 17 and Tomcat 10
- Creating global Class holder
- No method found for class java.lang.String in Kafka
- Issue edit a jtable with a pictures
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- Does the && (logical AND) operator have a higher precedence than || (logical OR) operator in Java?
- Mixed color rendering in a JTable
- HTTPS configuration in Spring Boot, server returning timeout
- How to use Layout to create textfields which dont increase in size?
- Function for making the code wait in javafx
- How to create beans of the same class for multiple template parameters in Spring
- How could you print a specific String from an array with the values of an array from a double array on the same line, using iteration to print all?
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Accessing Secret Variables in Classic Pipelines through Java app in Azure DevOps
- Postgres && statement Error in Mybatis Mapper?
Related Questions in GWT
- When accessing the page with gwt it is blank, it does not load
- Encountering bindException when running CodeServer
- How to get variable from GWT Callback
- IntelliJ Idea won't stop on breakpoints while GWT debugging in Super Dev Mode
- How to debug GWT project in Idea
- [Resolved]How to set up gwt super dev mode with the new gwt maven plugin with a web application using Errai framework?
- Integrate DominoKit/-UI Elements Into Existing GWT Application
- Avro after upgrading to JDK 17
- Draw graph in GWT
- How do I stop maven-jetty-plugin jetty:run output warnings: scanned from multiple locations
- Type incoherence when compiling Java code through GWT
- Application not loading with new GWT Maven Plugin
- Getting the error while updating GWT 2.5.1 to 2.11.0 and sencha GXT 3.1.1 to 4.1
- Playing an audio in the front-end without storing any cache to avoid illegal downloading
- how to add a static value to a FlexTable in the ui.xml file GWT?
Related Questions in GWT-2.8
- Debug Doesn’t Work After Migrating SmartGWT from v4.0 to v13.0
- GWT compile error: Rebind result cannot be abstract DOMUtilImpl
- Getting Uncaught RangeError: Maximum call stack size exceeded in GWT 2.9 post compilation
- GWT SimplePager style not working after migrating to GWT 2.8
- GWT Upgrade from 2.3 to 2.8.1
- Change loading sequence of style-sheet in GWT/SmartGWT
- Eclipse / launch / Java Classpath enclosed in quotes / breaks 3rd party tools / blocks development
- How to use XMLHttpRequest in GWT?
- What is the best way to customize or entirely skip GWT's *.nocache.js
- Unresponsive tabs in Chrome and Safari after upgrading to GWT 2.8
- GWT compilation error with Maven: No instance of Presenter is defined. Forget to annotate @Presenter or @EventHandler?
- GWT 2.8 cannot get custom linker to work
- GWT 2.8 dropevent datatransfer null
- calling gwt function from js file code.[Error is not a function]
- GWT Compiler : when is a compilation error fatal?
Related Questions in GWT-JSINTEROP
- The highcmaps library does not work if I have imported the highcstock library before it
- Problem efficiently mapping a native JavaScript library variable parameter API with Elemental2 / GWT?
- Using GWT Jsinterop to change already existing object created with GWT Js wrapper
- GWT / JSInterop / Eclipse Plugin / Production Build
- Call Java object from JS
- WebGPU JsInterop wrapper
- How to access window.opener while using gwt.jsinterop
- Javascript module function in GWT with JsInterop
- Should I stop using GSS etc. if I want to be ready for GWT 3?
- Controlling which classes GWT includes with -generateJsInteropExports
- GWT - using compiled code (JsInterop) in a service worker
- JsInterop for Java primitive object wrapper classes
- How does Js.cast() perform its type checking?
- GWT JsInterop - extending a Java interface in JavaScript
- Wrap a GWT widget in a web component
Related Questions in GWT-ELEMENTAL
- Convert a byte[] into a JavaScript Blob using elemental2
- Problem efficiently mapping a native JavaScript library variable parameter API with Elemental2 / GWT?
- Class Cast Exception when casting an elemental2.dom.Event to elemental2.dom.CustomEvent in GWT
- Is there a way to get an input value in Java - elemental2?
- How does Js.cast() perform its type checking?
- iOS 13 Safari Mobile - IndexedDB - AbortError - The Operation Was Aborted
- GWT elemental2.dom.PushSubscription Missing keys (p256dh and auth) in PushSubscription
- Bug? JsNumber toFixed returns different values in SuperDev and JS
- How to use XMLHttpRequest in GWT?
- Gwt elemental2: How can I convert between a gwt JavaScript object, and a JsInterop object?
- Does GWT elemental use JsType or JSNI Overlays?
- How to get a reference to elemental.html.FormData
- GWT Elemental convert elements
- How to use the rear camera in a mobile navigator using Gwt-Elemental 2.8
- Reference/Documentation for GWT Elemental webRTC api
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You haven't mentioned what GWT version you use, so I assume the latest one. It means 2.8.2 or newer.
Elemental2 is the way to go
As it is mentioned in comments above,
Elemental2is the right way. I will explain it a bit.If you think about future-proof implementation (being aware of
GWT3/J2CLnew approach), please do not use legacy GWT stuff. It means please useelemental2.dom.XMLHttpRequestinstead ofcom.google.gwt.xhr.client.XMLHttpRequest(the one mentioned by you). Please do not usegwt-userdependency if possible, as it will be deprecated (if it is not already).The
Elemental2is an opensource project available here: https://github.com/google/elemental2. It is kind of a base library for the "new GWT". For easier migration of existing GWT2.x projects to GWT3.x, a part of the "old"gwt-useris currently being ported to the new approach usingJsInteroptechnique and mentionedElemental2. So definitelyElemental2is the way to go.Elemental2 and JsInterop in general
The specification is not yet that rich if about the new JsInterop approach, but at the moment you will find some introduction at least: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJsInterop.html
Examples
Please find an example for
XMLHttpRequestin this article: http://www.g-widgets.com/2016/09/09/gwt-http-requests-alternatives/If you look for examples, also a good way is to search this on the Github site this way: https://github.com/search?q=elemental2.dom.XMLHttpRequest&type=Code.
(To use Github search you need to be logged in, in other case you will see "Whoa there! You have triggered an abuse blah blah..." )
One of the results will lead you to the very interesting project (you have the preview of the future
GWTnow): https://github.com/gwtproject/gwt-http. It is a future-proof port of the legacycom.google.gwt.http.HTTPGWT module. It will help to migrate GWT2.x projects to GWT3.x.When you look to the test package, you will find some examples: https://github.com/gwtproject/gwt-http/tree/master/src/test/java/org/gwtproject/http/client . So this is finally the answer to your question: "how to use it?" :-)
An additional examples source for
XMLHttpRequest(usingElemental2) from Gist: https://gist.github.com/search?utf8=%E2%9C%93&q=elemental2.dom.XMLHttpRequest. This is probably even better for start, as they are short and clear.What Elemental2 is?
The
Elemental2gives you a type checked access to native browser's API. So if you are familiar with browser's API, you should be able to implement your stuff, even based on some native JavaScript example. Please think about the new GWT like about type safe JavaScript (in addition very performant and well optimized). WithJsInteropyou create bindings, so it is something similar to bindings for TypeScript. So in fact you have a possibility to deal directly with browser's API, without anything GWT specific.Libraries? More examples...?
Dealing with
XMLHttpRequestis a bit low level.You have also a possibility to use the library. One of Github search results will lead you to this repository: https://github.com/ibaca/autorest-streaming-example which is an example for interesting REST library: https://github.com/intendia-oss/autorest. A modern and reactive one, works with Observables, RxJava and so on. This library uses
JsInteropand is also migrated toElemental2what makes itGWT3/J2CLready, please see the change: https://github.com/intendia-oss/autorest/commit/58516802cd42134544e6e3787207b5431fae94b5 .With the Github search query I provided you, now you are able to find even more code examples for
XMLHttpRequest. So please just have a look and find the best one for your needs.An alternative approach would be to use a framework, for instance Errai from RedHat: http://erraiframework.org/. It helps you to deal with many problems at a different abstraction level.
I think now you have some references to study.
On the other hand it's 2018, so why not the Fetch API?
When think about the modern web application, I would rather think about the
Fetch APIinstead ofXMLHttpRequest. All modern browsers now implement thefetch()function natively. Isn't it the best way for solving your issue then? Thefetch()is a Promise-based mechanism that allows you to make network requests similar toXMLHttpRequest. Promises and Fetch are handled by Elemental2. Then you can use it from your Java code more or less in similar way like in Mozilla's examples.Read more about the
Fetch APIhere:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
https://developers.google.com/web/updates/2015/03/introduction-to-fetch
https://codepen.io/aderaaij/post/fetching-data-with-fetch
https://fetch.spec.whatwg.org/
What more, this is nothing new as you see. If about the older browsers a
polyfillwill emulate the missing function: https://github.com/github/fetch.If about examples, I don't see that much on Github: https://github.com/search?utf8=%E2%9C%93&q=elemental2.dom.DomGlobal+fetch&type=Code, but at least something.
The
Fetch APIseems to be the most current solution to the problem.Please find a very simple fetch() example using Elemental2.
The imports section:
Then use in your code:
As a result you should be able to see something like this:
You can further convert the result to Java object using a technique called JsInterop DTOs. If you are interested, please find some information here: https://stackoverflow.com/a/50565283/5394086 .
Not recommended approach
If you sadly prefer to use the old GWT, so <= 2.7, then I think you can search for some examples on Github using similar search query, but for this legacy com.google.gwt.xhr.client.XMLHttpRequest. I this case I would also suggest you to not do stuff so low level, but use a library like https://github.com/reinert/requestor (which is unfortunately discontinued and development has stopped on GWT 2.7, but for this GWT version it is probably the best choice). But again please do not go this way and use GWT >= 2.8.2 with
Elemental2/JsInteropapproach instead.