欢迎光临散文网 会员登陆 & 注册

axios 基本使用及跨域

2023-07-06 11:15 作者:swotpp  | 我要投稿

安装使用:

https://axios-http.com/docs/intro

https://www.npmjs.com/package/axios

安装:

    npm install axios

使用:

跨域问题

如果出现跨域问题会报:

Access to XMLHttpRequest at 'url' form origin 'url2' has been blocked by CORS policy: NO 'Access-Control-Allow-Origin' header is present on the requested resource.

不跨域的要求是:发起者即客户端的协议名、主机名、端口号与服务器三者一致。

发生跨域后是浏览器没把服务器返回的数据给开发者,而是直接显示了上面的错误信息给开发者。

1. 解决跨域问题方式一 不灵活:

[NOTE]

====

* proxy 属性值是后台服务器的根 url

* 在 vue-cli 启动的前端项目中没有的资源,才会被转发到代理服务器

* 该方式的缺点是只能配置 ##一个代理##

====

2. 解决跨域问题方式二 好用

[NOTE]

====

pathRewrite:{'^/api':''}  发给后端 target 服务器时将 'api' 替换为 ''

ws:true 用于支持 websocket

changeOrigin: true/false, Default: false 

* changes the origin of the host header to the target URI

* 设置为 true,则后台服务器打印 request.host 时,显示内容和服务器一样。

====


axios 基本使用及跨域的评论 (共 条)

分享到微博请遵守国家法律