Referer与图片防盗链

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lhq186/article/details/79851494

1,axios用法   转自 https://www.cnblogs.com/wisewrong/p/6402183.html 

方案一:改写原型链

首先在 main.js 中引入 axios

import axios from 'axios'

这时候如果在其它的组件中,是无法使用 axios 命令的。但如果将 axios 改写为 Vue 的原型属性,就能解决这个问题

Vue.prototype.$ajax = axios

在 main.js 中添加了这两行代码之后,就能直接在组件的 methods 中使用 $ajax 命令

复制代码
methods: {
  submitForm () {
    this.$ajax({
      method: 'post',
      url: '/user',
      data: {
        name: 'wise',
        info: 'wrong'
      }
   })
}
复制代码


方案二:在 Vuex 中封装

之前的文章中用到过 Vuex 的 mutations,从结果上看,mutations 类似于事件,用于提交 Vuex 中的状态 state

action 和 mutations 也很类似,主要的区别在于,action 可以包含异步操作,而且可以通过 action 来提交 mutations

另外还有一个重要的区别:

mutations 有一个固有参数 state,接收的是 Vuex 中的 state 对象

action 也有一个固有参数 context,但是 context 是 state 的父级,包含  state、getters

 

Vuex 的仓库是 store.js,将 axios 引入,并在 action 添加新的方法

复制代码
// store.js
import Vue from 'Vue' import Vuex from 'vuex' // 引入 axios import axios from 'axios' Vue.use(Vuex) const store = new Vuex.Store({ // 定义状态 state: { test01: { name: 'Wise Wrong' }, test02: { tell: '12312345678' } }, actions: { // 封装一个 ajax 方法 saveForm (context) { axios({ method: 'post', url: '/user', data: context.state.test02 }) } } }) export default store
复制代码

注意:即使已经在 main.js 中引入了 axios,并改写了原型链,也无法在 store.js 中直接使用 $ajax 命令

换言之,这两种方案是相互独立的

 

在组件中发送请求的时候,需要使用 this.$store.dispatch 来分发

methods: {
  submitForm () {
    this.$store.dispatch('saveForm')
  }
}

submitForm 是绑定在组件上的一个方法,将触发 saveForm,从而通过 axios 向服务器发送请求


附录:配置 axios 

上面封装的方法中,使用了 axios 的三个配置项,实际上只有 url 是必须的,完整的 api 可以参考使用说明

为了方便,axios 还为每种方法起了别名,比如上面的 saveForm 方法等价于:

axios.post('/user', context.state.test02)

完整的请求还应当包括 .then 和 .catch

.then(function(res){
  console.log(res)
})
.catch(function(err){
  console.log(err)
})

当请求成功时,会执行 .then,否则执行 .catch

这两个回调函数都有各自独立的作用域,如果直接在里面访问 this,无法访问到 Vue 实例

这时只要添加一个 .bind(this) 就能解决这个问题

.then(function(res){
  console.log(this.data)
}.bind(this))


axios不支持vue.use()方式声明使用,看了所有近乎相同的axios文档都没有提到这一点 

https://blog.csdn.net/u012369271/article/details/72848102

建议方式

在main.js中如下声明使用
import axios from 'axios';
Vue.prototype.$axios=axios;
那么在其他vue组件中就可以this.$axios调用使用

-------------------->>>>>>>

2018.4.8   遇到的问题


mounted() {
// mounted 是创建完了之后,进行的数据挂载 ,数据绑定之类的
// axios.get('/static/test.json').then(function(rsp)
this.$axios.get( 'https://news-at.zhihu.com/api/4/news/latest').then( function(rsp){
console.log(rsp.data);
}).catch((error) => {
console.log(error);
})
}

出现报错

Failed to load https://news-at.zhihu.com/api/4/news/latest: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8088' is therefore not allowed access.

换成了另一个托管的网址后,https://zhihu-daily.leanapp.cn/api/v1/last-stories

就可以了,猜测问题在于知乎日报做了防跨域访问设置


跟着学习的视频,作者在后面给了新的办法,然后换了网址就可以访问了


1,装allow-control-allow-origin:* 插件,允许跨域操作

2,在index.html写 <meta name="referrer" content="never"> 不许被追踪

这句话应对知乎的漏洞,可以隐藏自己的地址,然后就可以不被追踪到,

防盗链设置     ----- 

Referer与图片防盗链

https://blog.csdn.net/baochao95/article/details/52673314 

使用 Referer Meta 标签控制 referer—详解 referrer-policy

 
http://blog.sina.com.cn/s/blog_9080de950102w2mk.html








猜你喜欢

转载自blog.csdn.net/lhq186/article/details/79851494
今日推荐