我正在使用vue2和axios进行ajax调用。在页面中,我正在调用各种ajax调用,其中一些使用HTTPS,而其他一些则通过HTTP进行,尽管两个代码是相似的。
例:
axios.get('/api/' + app.$variable1 + '/get-something/')
.then(({ data }) =>
{
app.array = [];
for(let i = 0; i < data.length; i++)
{
app.vats.push({
id:data[i]['id'],
name:data[i]['name'],
something_else[i]['something_else']
});
}
})
Run Code Online (Sandbox Code Playgroud)
问题:如何强制Axios采用HTTPS?
Objs:我无法手动添加https,例如:“ https://www.example.com/1234/12 ”,因为我使用的是相对网址(我在网址中分配了某些ID,然后重复使用它们进行通话)。
服务器:1)我通过htaccess强制使用Https 2)我也在使用安全标头,该标头不允许浏览器脱离“自我”
编辑:
因此,尝试着解决这个问题:1)在Mounted方法中,我调用了4个单独的API。前两个由于HTTP而失败,后两个通过。我尝试更改命令,它始终是前两个失败的命令。我试图将代码移至Created,这意义不大,并且确定它没有起作用。
救命!!
小智 1
添加axios 请求拦截器并将config.urlhttp 更改为 https。所有请求都将被拦截,您将可以访问基本 URL 方案。
const instance = axios.create({
baseURL: 'http://api.com',
})
instance.interceptors.request.use(function(config) {
// change the url scheme from http to https
config.url = config.url.replace('http://', 'https://')
return config
})
Run Code Online (Sandbox Code Playgroud)