nativescript-http
默认的nativescript http模块,包含认证失败回调等。
npm i --save nativescript-http

安装

将nativescript-http安装到现有的NativeScript项目中

npm i nativescript-http

用法

核心(全局)

如果您正在使用TypeScript,请将以下内容添加到您的app.ts文件中。

app.ts
declare global {
// eslint-disable-next-line no-unused-vars
module NodeJS {
// eslint-disable-next-line no-unused-vars
interface Global {
http: HttpClient;
}
}
}

全局添加http模块

app.ts
import { HttpClient } from 'nativescript-http'

global.http = new HttpClient({
baseUrl: 'https://achrafbardan.me', // optional
unauthenticatedCallback: (response) => { // optional
console.log(response.content);
},
// When an http response code is inside this array the above function will run
unauthenticatedStatusCodes: [201] // optional
});

发起请求

main-page-model.ts
makeRequest() {
global.http.request({
method: 'get',
url: '/users',
content: JSON.stringify({
id: 1
}),
dontFollowRedirects: false,
headers: {
"Content-Type": "application/json"
},
timeout: 0
}).then(response => {
console.log(response.content)
})
}