当前位置:   article > 正文

Vue 安装 axios 和使用_vue安装并使用axios

vue安装并使用axios

1最开始一直提示400错误,根据下面的测试可成功安装

  1. 1、查看代理设置执行,不为null时设置为空:
  2. npm config get proxy
  3. npm config get https-proxy
  4. 如果返回值不为null,继续执行:
  5. (这一步很重要,一定要保证两个命令的返回值都为null
  6. npm config set proxy null
  7. npm config set https-proxy null
  8. 2、执行:
  9. npm config set registry http://registry.cnpmjs.org/

2安装axios

npm install axios

3.设置代理服务器,可发送请求和解决跨域问题,跨域可通过(cors,jsonp,代理服务器)现在通过cli的代理服务器实现ajax请求。

   一般使用多个后台实现方式。

  1. vue.config.js
  2. module.exports = defineConfig({
  3. transpileDependencies: true,
  4. //使用cli 脚手架 配置一个代理服务器 可解决跨域问题。
  5. //一个后台源
  6. devServer: {
  7. proxy: 'http://localhost:1230'
  8. }
  9. //多个后台源
  10. devServer: {
  11. proxy: {
  12. '/api': {
  13. target: 'http://localhost:1230',
  14. pathRewrite:{'^/api':''},//替换路径中的/api
  15. ws: true,
  16. changeOrigin: true,//高速服务器请求的端口号是真的还是
  17. },
  18. '/foo': {
  19. target: 'http://localhost:1230'
  20. }
  21. }
  22. }
  23. })

发送请求

  1. <script>
  2. import f from '@/Content/php'//默认暴露
  3. import axios from 'axios';//引用axios
  4. export default {
  5. name: 'myMsg',
  6. data() {
  7. return { Message: '' };
  8. },
  9. methods: {
  10. GetStudents:function(){
  11. f.funHello1('php');
  12. f.funHello2('张三');
  13. },
  14. Getaxiosfun:function(){
  15. //使用axios
  16. axios.get('http://localhost:8080/api/Home/GetStudents').then(
  17. response=>{alert(this.Message=response.data)},//成功回调
  18. error=>{alert(this.Message=error.message)},//失败回调
  19. );
  20. }
  21. },
  22. };
  23. </script>

Post

  1. Postaxiosfun:function(){
  2. //使用post
  3. axios.post('http://localhost:8080/api/Home/GetStudents2',{name:'0123456'}).then(
  4. response=>{
  5. alert(this.Message=response.data)
  6. },//成功回调
  7. error=>{
  8. alert(this.Message=error.message)
  9. },//失败回调
  10. );
  11. }

c#接口

  1. [HttpGet]
  2. public string GetStudents(string name) {
  3. return 'get:' + name;
  4. }
  5. [HttpPost]
  6. public string GetStudents2(string name)
  7. {
  8. return 'post:'+name;
  9. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/466070
推荐阅读
相关标签
  

闽ICP备14008679号