方式一:直接在代码中使用完整url进行访问:
@action getData = (page: number) => {
axios.get(`http://111.231.0.208/api/course?limit=20&offset=${(page - 1) * 20}`).then(res => {
this.data.page = page
this.data.list = res as any
})
}
这样是完全跑通的,但是这是个前端项目会涉及到跨域问题。
而一般的在前端项目中的做法,都是在webpack中做一层转发,把本地url映射到服务器上,代码如下:
devServer: {
hot: true,
port: localPort,
host:'0.0.0.0',
// enable HMR on the server
noInfo: true,
quiet: false,
historyApiFallback: true,
// minimize the output to terminal.
contentBase: resolve(rootPath, 'src'),
// match the output path
publicPath: '/',
// match the output `publicPath`
proxy: {
"/api/**": "http://111.231.0.208/"
}
},
而一旦通过这种方式去请求/api/course接口,这个请求就会被网关拦截,而且通过fiddler可以看到被拦截的地址是localhost:3000/api/course,并且没有发出http://111.231.0.208/api/cour...,是不是可以理解为在请求这个本地路径时,请求就被拦截了。那他是如何做到的?
fiddler是拦截不到代理服务器发出的请求的,
所以你得弄清这个错误是代理服务器响应的,还是
111.231.0.208
通过代理服务器响应给你的。