Skip to content

Latest commit

 

History

History
37 lines (25 loc) · 1.86 KB

File metadata and controls

37 lines (25 loc) · 1.86 KB

Requests

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.

Usage

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",
	...
},

Use case: making a request to an endpoint that returns JSON

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.

More information.

Please visit the MDN documentation on the fetch API for more information.