|
| 1 | +import axios from 'axios' |
| 2 | +import mpAdapter from 'axios-miniprogram-adapter' |
| 3 | +axios.defaults.adapter = mpAdapter |
| 4 | + |
| 5 | +// 创建实例 设置baseURL |
| 6 | +const instance = axios.create({ |
| 7 | + baseURL: 'https://wx.jk724.com' |
| 8 | +}) |
| 9 | + |
| 10 | +Page({ |
| 11 | + async handleRequestByGet() { |
| 12 | + my.showLoading(); |
| 13 | + try { |
| 14 | + const resp = await instance.get('/api/HotSearchWords') |
| 15 | + console.log('GET请求成功:', resp) |
| 16 | + |
| 17 | + } catch (error) { |
| 18 | + // |
| 19 | + } finally { |
| 20 | + my.hideLoading(); |
| 21 | + } |
| 22 | + }, |
| 23 | + async handleRequestByPOST() { |
| 24 | + my.showLoading(); |
| 25 | + try { |
| 26 | + const resp = await instance.post('https://t.captcha.qq.com/cap_union_new_verify') |
| 27 | + console.log('POST请求成功:', resp) |
| 28 | + |
| 29 | + } catch (error) { |
| 30 | + // |
| 31 | + } finally { |
| 32 | + my.hideLoading(); |
| 33 | + } |
| 34 | + }, |
| 35 | + // get请求带参数 |
| 36 | + async handleRequestByGetParams() { |
| 37 | + my.showLoading(); |
| 38 | + try { |
| 39 | + const resp = await instance({ |
| 40 | + url: '/api/HotSearchWords', |
| 41 | + params: { |
| 42 | + name: '我是参数' |
| 43 | + } |
| 44 | + }) |
| 45 | + console.log('get带参数请求成功:', resp) |
| 46 | + |
| 47 | + } catch (error) { |
| 48 | + // |
| 49 | + } finally { |
| 50 | + my.hideLoading(); |
| 51 | + } |
| 52 | + }, |
| 53 | + // post请求带参数(注意查看请求头的 content-type: application/json 和 请求体的 Reuquest Payload) |
| 54 | + async handleRequestByPOSTParams() { |
| 55 | + my.showLoading(); |
| 56 | + try { |
| 57 | + const resp = await instance({ |
| 58 | + method: 'POST', |
| 59 | + url: 'https://t.captcha.qq.com/cap_union_new_verify', |
| 60 | + data: { |
| 61 | + name: '我是参数' |
| 62 | + } |
| 63 | + }) |
| 64 | + console.log('post带参数请求成功:', resp) |
| 65 | + |
| 66 | + } catch (error) { |
| 67 | + // |
| 68 | + } finally { |
| 69 | + my.hideLoading(); |
| 70 | + } |
| 71 | + }, |
| 72 | + // 自定义请求头参数 |
| 73 | + async handleRequestByHeaders() { |
| 74 | + my.showLoading(); |
| 75 | + try { |
| 76 | + const resp = await instance({ |
| 77 | + method: 'GET', |
| 78 | + url: '/api/HotSearchWords', |
| 79 | + headers: { |
| 80 | + Authorization: 'im a token' |
| 81 | + } |
| 82 | + }) |
| 83 | + console.log('post带参数请求成功:', resp) |
| 84 | + |
| 85 | + } catch (error) { |
| 86 | + // |
| 87 | + } finally { |
| 88 | + my.hideLoading(); |
| 89 | + } |
| 90 | + }, |
| 91 | + // 修改content type (注意查看请求头的 content-type: application/x-www-form-urlencoded 和 请求体的 form data) |
| 92 | + async handleRequestByContentType() { |
| 93 | + my.showLoading(); |
| 94 | + try { |
| 95 | + const resp = await instance({ |
| 96 | + method: 'POST', |
| 97 | + url: 'https://t.captcha.qq.com/cap_union_new_verify', |
| 98 | + headers: { |
| 99 | + 'content-type': 'application/x-www-form-urlencoded' |
| 100 | + }, |
| 101 | + data: { |
| 102 | + name: '我是参数' |
| 103 | + } |
| 104 | + }) |
| 105 | + console.log('post带参数请求成功:', resp) |
| 106 | + |
| 107 | + } catch (error) { |
| 108 | + // |
| 109 | + } finally { |
| 110 | + my.hideLoading(); |
| 111 | + } |
| 112 | + } |
| 113 | +}); |
0 commit comments