首页 > 其他分享 >vue框架跨域请求之axios

vue框架跨域请求之axios

时间:2022-11-11 20:58:38浏览次数:80  
标签:domain axios false 跨域 vue path true option

1、打开hbuilderx,【文件】==》【新建】==》【项目】,创建vue项目。

 


 

 2、创建后的vue项目结构如下所示,打开【package.json】配置文件,添加axios相关依赖。

 

 

 

 


 

 3、删除【node_modules】文件夹及【package-lock.json】文件。

 


 

 4、右键项目,【外部命令】==》【npm install】下载相关依赖模块。

 


 

 5、在【package.json】同级目录下创建【vue.config.js】文件。

 

 

 

 


 

 

6、对【vue.config.js】进行配置,配置代理。

配置代理参数项解释:
The following options are provided by the underlying http-proxy library.

option.target: url string to be parsed with the url module

option.forward: url string to be parsed with the url module

option.agent: object to be passed to http(s).request (see Node's https agent and http agent objects)

option.ssl: object to be passed to https.createServer()

option.ws: true/false: if you want to proxy websockets

option.xfwd: true/false, adds x-forward headers

option.secure: true/false, if you want to verify the SSL Certs

option.toProxy: true/false, passes the absolute URL as the path (useful for proxying to proxies)

option.prependPath: true/false, Default: true - specify whether you want to prepend the target's path to the proxy path

option.ignorePath: true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request (note: you will have to append / manually if required).

option.localAddress : Local interface string to bind for outgoing connections

option.changeOrigin: true/false, Default: false - changes the origin of the host header to the target URL

option.preserveHeaderKeyCase: true/false, Default: false - specify whether you want to keep letter case of response header key

option.auth : Basic authentication i.e. 'user:password' to compute an Authorization header.

option.hostRewrite: rewrites the location hostname on (301/302/307/308) redirects.

option.autoRewrite: rewrites the location host/port on (301/302/307/308) redirects based on requested host/port. Default: false.

option.protocolRewrite: rewrites the location protocol on (301/302/307/308) redirects to 'http' or 'https'. Default: null.

option.cookieDomainRewrite: rewrites domain of set-cookie headers. Possible values:

false (default): disable cookie rewriting

String: new domain, for example cookieDomainRewrite: "new.domain". To remove the domain, use cookieDomainRewrite: "".

Object: mapping of domains to new domains, use "*" to match all domains.
For example keep one domain unchanged, rewrite one domain and remove other domains:

cookieDomainRewrite: {
  "unchanged.domain": "unchanged.domain",
  "old.domain": "new.domain",
  "*": ""
}
option.cookiePathRewrite: rewrites path of set-cookie headers. Possible values:

false (default): disable cookie rewriting

String: new path, for example cookiePathRewrite: "/newPath/". To remove the path, use cookiePathRewrite: "". To set path to root use cookiePathRewrite: "/".

Object: mapping of paths to new paths, use "*" to match all paths. For example, to keep one path unchanged, rewrite one path and remove other paths:

cookiePathRewrite: {
  "/unchanged.path/": "/unchanged.path/",
  "/old.path/": "/new.path/",
  "*": ""
}
option.headers: object, adds request headers. (Example: {host:'www.example.org'})

option.proxyTimeout: timeout (in millis) when proxy receives no response from target

option.timeout: timeout (in millis) for incoming requests

option.followRedirects: true/false, Default: false - specify whether you want to follow redirects

option.selfHandleResponse true/false, if set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the proxyRes event

option.buffer: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:

 

 


 

 7、配置【main.js】文件

 


 

 8、配置【App.vue】,配置如下

<template>
  <div id="app">
    <!-- <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/> -->
    <input :type="type" :value="value" @click="axiosHandler()" />
  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'

/* export default {
  name: 'app',
  components: {
    HelloWorld
  }
} */

export default{
    data(){
        return{
            type:"button",
            value:"axios test"
        }
    },
    methods:{
        axiosHandler(){
            this.$axios.get("/api/s?ie=UTF-8&wd=test")
            .then(res=>{
                console.log("axios 跨域请求开始!");
                console.log(res);
                console.log("axios 跨域请求结束!");
            })
            .catch(error=>{
                console.log(error);
            });
        }
    }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

 


 

 

9、右键项目【外部命令】==》【npm run serve】,运行项目。然后点击【axios test】按钮,查看效果。

 

 

 

 

 

标签:domain,axios,false,跨域,vue,path,true,option
From: https://www.cnblogs.com/lightbc/p/16878984.html

相关文章

  • VueRouter笔记 - 路由守卫
    路由守卫目录路由守卫1.路由守卫简介2.全局前置守卫3.全局后置路由守卫4.独享路由守卫5.组件内路由守卫1.路由守卫简介路由守卫主要用来通过跳转或取消的方式守......
  • VueRouter笔记 - VueRouter基础
    VueRouter目录VueRouter1.VueRouter简介1.1路由的基础实现步骤1.2注意事项2.嵌套路由3.命名路由4.重定向和别名4.1重定向4.2别名5.编程式路由导航5.1使用router......
  • VueRouter笔记 - 路由参数(query/params/props/meta)
    路由参数目录路由参数1.query2.params参数3.props参数4.meta参数1.queryquery可以用于在不同路由之间传递数据(大多数是父传子)一般网页在跳转时显示的链接,?后......
  • vue element 表单自定义效验 及 单独效验某个值
    主要功能 (表单自定义效验 && 单独效验某个值)表单自定义效验exportdefault{data(){//定义效验constvalidatePersonCharge=(rul......
  • Vue 怎么用 vm.$set() 解决对象新增属性不能响应的问题 ?
    Vue提供了Vue.set(object,propertyName,value)/vm.$set(object,propertyName,value)来实现为对象添加响应式属性。vm.$set的实现原理是:如果目标是数组,直接......
  • space 动态布局算法(vue3-ts、setup)
    动态布局组件演示效果<template><ulclass="space_ulflex-row":style="{'padding-top':`${hs}px`,'padding-left':`${ws}px`}"......
  • Vue3中的新的内置组件
    在vue2中的内置组件:动态路由中的component:作用:动态显示路由的挂载点,使用is属性动态显示组件;keep-alive:作用:使被包裹的组件保留状态,避免被重新......
  • parceljs 2.8 整合 vue 及解决其他 bug
    问题vue2打包报错https://blog.csdn.net/C_ZhangSir/article/details/100989902解决:引入importVuefrom'vue/dist/vue';parcel中文路径导致打包出错https://blo......
  • vue-codemirror 代码编辑器
    codemirror是一个非常强大的代码编辑器插件,但官方并没有提供vue的支持版本,不过跟vue集成的步骤并不复杂,以下是具体实现更多精彩更多技术博客,请移步IT人才终生实训......
  • vue_01_函数式组件
    函数式组件-全局弹窗组件函数式组件定义1.在template上写上functional2.在exportdefault{}中设置functional为true//方式一<templatefunctional><temp......