Making requests is a common task in the frontend world.
In liferay-portal
, we provide the fetch
function which is a very thin wrapper on top of the existing fetch
function implemented in modern browsers. Additionally, a polyfill is also served for Internet Explorer 11, to ensure the fetch
function is defined.
Make sure you add the frontend-js-web
dependency to your package.json
file, and that the version corresponds to the one declared in the frontend-js-web/bnd.bnd
file.
"dependencies": {
...
"frontend-js-web": "4.0.0",
...
},
import {fetch} from 'frontend-js-web';
const url = 'http://localhost:8080/path/to/an/existing/endpoint';
fetch(url)
.then(res => res.json())
.then(res => {
// Use the data returned by the request
});
NOTE: In case you aren't using modules (i.e. import
), you can access the fetch
wrapper through the Liferay.Util.fetch
method.
Please visit the MDN documentation on the fetch API for more information.