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 }