Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重识vue-resource #8

Open
xixigiggling opened this issue Jun 28, 2018 · 0 comments
Open

重识vue-resource #8

xixigiggling opened this issue Jun 28, 2018 · 0 comments

Comments

@xixigiggling
Copy link
Owner

由于一直以为vue-resource,改不了baseUrl以及像利用axios来每次请求都在头部自动加上JWT_TokenVue 进阶教程之:Axios配置JWT/封装插件/发送表单数据),再加上就连官方都没推荐了,就一直打入冷宫,突然这次因为axios,自己遇到一些跨域的问题,不知道到底是什么回事,就又换回了vue-resource,现在才发现,只是自己不懂...这些原来都有

baseUrl

//Set default values using the global configuration
Vue.http.options.root = 'http://47.106.111.100:8088';

// Set default values inside your Vue component options.
new Vue({

  http: {
    root: '/root',
    headers: {
      Authorization: 'Basic YXBpOnBhc3N3b3Jk'
    }
  }

})

神奇的是:

// 只开这个即可
Vue.http.options.root = 'http://47.106.111.100:8088';

而在vue实例中开不开http选项都没关系...

//测试: 在`main.js`中写入
new Vue({
    el: '#app',
    router,
    http: {
        root: 'http://47.106.111.100:8088',
    },
    components: {App},
    template: '<App/>',
});

另一个注意事项就是:

Note that for the root option to work, the path of the request must be relative

Vue.http.get('someUrl') while this will not: Vue.http.get('/someUrl')

也就是在写成this.$http.post('/rest/app/register', test)的时候是不行的,必须要在url中写成'rest/app/register'

头部自动加上JWT_Token

同上所示: Vue.http.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk';

另还有不支持json,要application/x-www-form-urlencoded的MIME type的转化:Vue.http.options.emulateJSON = true;

结论是:好好看文档

参考链接:

Configuration-官方文档

vue-resource headers如何动态地设置

@xixigiggling xixigiggling added the Vue Vue相关的问题 label Jun 28, 2018
@xixigiggling xixigiggling added 已归档 已放入目录中 项目的一些探索和发现 and removed Vue Vue相关的问题 labels Jan 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant