- Source:
Methods
createJsonRequest(url, optionsopt, nullable, useNativeopt, strictopt, nullable) → {Requests.JsonFetchRequest}
This method creates a special version of a FetchRequest specifically designed to retrieve JSON data.
Usually you'll want to retrieve JSON as a PlainObject, so that's the default resolve value here. However, you may also specify to retrieve the raw JSON or let the method handle the creation of a DOM element for you and return that, ready to be used/inserted however you like.
If you plan on inserting the result into DOM anyway you'll like the fact that this is also directly possible, by defining an insert target. BTW: Inserting does not automatically change the resolve value, those are separate concerns.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
url |
String | the complete URL to query |
||
options |
Object |
<optional> <nullable> |
null | the request options (see: createFetchRequests for details) |
useNative |
Boolean | String |
<optional> |
false | determines if the native Fetch implementation of the browser should be used, true forces usage, "auto" uses it only if available |
strict |
Boolean |
<optional> <nullable> |
true | if true, enforces "application/json" as accept header as well as response mime type, if false, accept header is not set and different mime type only results in warning |
- Source:
- See:
Throws:
error in strict mode if response content type is not "application/json"
Returns:
Example
createJsonRequest('message.json').execute()
.then(json => { alert(json.someProperty); })
;
createJsonRequest('/dev/config.json')
.execute('element', null, 'config-json-id')
.then(jsonElement => { document.querySelector('main').appendChild(jsonElement); })
;
createJsonRequest('https://foobar.com/config.json')
.execute('raw', {element : document.body, position : 'prepend'}, 'config-json-id')
.then(rawJson => { console.log(`"${rawJson}" has been inserted at the beginning of the document's body`); })
;