cross-domain

 1   // GET
 2   jsonp = () => {
 3     let url = "http://xxx";
 4     fetch(url).then(function (response) {
 5       return response.json()
 6     }).then((json) => {
 7       console.log('parsed json', json);
 8       document.getElementById("demo").innerHTML = json.result[0].title;
 9     }).catch(function (ex) {
10       console.log('parsing failed', ex)
11     })
12   };
13   jsonp()
 1   // POST
 2   jsonp = () => {
 3     let postData = {
 4       account: 'xxx',
 5       password: 'xxx',
 6       name: 'xx',
 7       url_back: 'http://xxxxx'
 8     };
 9     fetch('http://xxxxx', {
10       method: 'POST',
11       mode: 'cors',
12       credentials: 'include',
13       headers: {
14         'Content-Type': 'application/x-www-form-urlencoded'
15       },
16       body: JSON.stringify(postData)
17     }).then(function (response) {
18       console.log(response);
19     });
20   };
21   jsonp()

 

 

axios

main.js

1 import axios from 'axios'
2 
3 Vue.prototype.$axios = axios
4 axios.defaults.baseURL = '/api'
5 axios.defaults.headers.post['Content-Type'] = 'application/json'

修改config文件夹下的index.js文件,在proxyTable中加上如下代码:

1 proxyTable: {
2   '/api':{
3     target: "https://c.y.qq.com/v8/fcg-bin",
4     changeOrigin:true,
5     pathRewrite:{
6       '^/api':''
7     }
8   }
9 },

请求

 1   methods: {
 2     _getRecommend () {
 3       this.$axios.get('/fcg_myqq_toplist.fcg', {
 4         params: {
 5           g_tk: 5381,
 6           uin: 0,
 7           format: 'json',
 8           inCharset: 'utf-8',
 9           outCharset: 'utf-8',
10           notice: 0,
11           platform: 'h5',
12           needNewCode: 1
13         },
14         headers: {'authorization': this.token}
15       }).then((res) => {
16         console.log(res.data)
17       }).catch((err) => {
18         console.log(err)
19       })
20     }
21   },
22   mounted () {
23     this._getRecommend()
24   }

 

转载于:https://www.cnblogs.com/ronle/p/10753655.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值