Get Response from URL

fetch("https://httpbin.org/get").then(function (r) {
    // Argument (r) is Response!
}).catch(failOnError(done));

Get string from URL

fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) {
    // Argument (r) is string!
}).catch(failOnError(done));

Get JSON from URL

fetch("https://httpbin.org/get").then(response => { return response.json(); }).then(function (r) {
    // Argument (r) is JSON object!
}).catch(failOnError(done));

Get FormData from URL

fetch("https://httpbin.org/get").then(response => { return response.formData(); }).then(function (r) {
    // Argument (r) is FormData object!
}).catch(failOnError(done));

Get Response status

fetch("https://httpbin.org/get").then(function (response) {
    // Argument (response) is Response!
    var statusCode = response.status;
}).catch(failOnError(done));

Get response headers

fetch("https://httpbin.org/get").then(function (response) {
    // Argument (response) is Response!
    // var all = response.headers.getAll();
}).catch(failOnError(done));

Post JSON

fetch("https://httpbin.org/post", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })
}).then(r => { return r.json(); }).then(function (r) {
    // console.log(result);
}).catch(failOnError(done));

Stay connected with NativeScript

(expect a newsletter every 4-8 weeks)

NativeScript
NativeScript is licensed under the Apache 2.0 license .
© Progress Software Corporation. All Rights Reserved.