MCPcopy Create free account
hub / github.com/bailicangdu/node-elm / fetch

Method fetch

prototype/baseComponent.js:19–60  ·  view source on GitHub ↗
(url = '', data = {}, type = 'GET', resType = 'JSON')

Source from the content-addressed store, hash-verified

17 this.qiniu = this.qiniu.bind(this)
18 }
19 async fetch(url = '', data = {}, type = 'GET', resType = 'JSON'){
20 type = type.toUpperCase();
21 resType = resType.toUpperCase();
22 if (type == 'GET') {
23 let dataStr = ''; //数据拼接字符串
24 Object.keys(data).forEach(key => {
25 dataStr += key + '=' + data[key] + '&';
26 })
27
28 if (dataStr !== '') {
29 dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
30 url = url + '?' + dataStr;
31 }
32 }
33
34 let requestConfig = {
35 method: type,
36 headers: {
37 'Accept': 'application/json',
38 'Content-Type': 'application/json'
39 },
40 }
41
42 if (type == 'POST') {
43 Object.defineProperty(requestConfig, 'body', {
44 value: JSON.stringify(data)
45 })
46 }
47 let responseJson;
48 try {
49 const response = await fetch(url, requestConfig);
50 if (resType === 'TEXT') {
51 responseJson = await response.text();
52 }else{
53 responseJson = await response.json();
54 }
55 } catch (err) {
56 console.log('获取http数据失败', err);
57 throw new Error(err)
58 }
59 return responseJson
60 }
61 //获取id列表
62 async getId(type){
63 if (!this.idList.includes(type)) {

Callers 5

guessPositionMethod · 0.80
searchPlaceMethod · 0.80
getDistanceMethod · 0.80
geocoderMethod · 0.80
getpoisMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected