• Vue2.js使用 Axios


    先安装

    npm install axios

    引入

    import axios from 'axios'

    跨域出现的问题

     第一种方式  在客户端修改脚手架的配置  代理服务器

    向客户端本身创建一个代理服务器  再让代理服务端向 服务端传递就不存在跨域问题了

    代理服务器的方式 在congfig.js里面添加 devServer

     代理服务器需要访问多个服务器不同的api时需要

    1. 每次修改vue.config.js都需要保存后重新启动一下项目
    2. 客户端请求的api会分别找到找到不同的api1和api2 
    3. 自己测试的时候(请求的两个服务器)时要查看服务器是否都打开

    第二种方式 在服务器段修改跨域问题

    安装cors: npm i cors                在使用就可以了

    测试:

    简单的一个服务端可以实现跨域请求就行

    1. const express = require('express')
    2. const app = express()
    3. // body-parser是一个HTTP请求体解析的中间件,使用这个模块可以解析JSON、Raw、文本、URL-encoded格式的请求体,
    4. const bodyparser = require('body-parser')
    5. app.use(bodyparser.json())
    6. // const cors = require('cors')
    7. // // 配置中间键
    8. // app.use(cors())
    9. app.get('/hello', (req, res) => {
    10. res.writeHead(200, { 'Content-Type': 'text/plain' })
    11. res.end('hello world')
    12. })
    13. // get 请求 user
    14. app.get('/user/:id', (req, res) => {
    15. const obj = {
    16. id: req.params.id,
    17. firstName: "张",
    18. lastName: "四"
    19. }
    20. res.writeHead(200, { 'Content-Type': 'text/plain;charset=utf-8' })
    21. res.end(JSON.stringify(obj))
    22. })
    23. // post 请求 user
    24. app.post('/user', (req, res) => {
    25. const obj = {
    26. firstName: req.body.first_name,
    27. lastName: req.body.last_name
    28. }
    29. console.log(obj);
    30. res.writeHead(200, { 'Content-Type': 'text/plain;charset=utf-8' })
    31. res.end(JSON.stringify(obj))
    32. })
    33. +
    34. // put() 修改请求 user
    35. app.put('/user/:id', (req, res) => {
    36. const obj = {
    37. id: req.params.id,
    38. firstName: req.body.first_name,
    39. lastName: req.body.last_name
    40. }
    41. console.log(obj);
    42. res.writeHead(200, { 'Content-Type': 'text/plain;charset=utf-8' })
    43. res.end(JSON.stringify(obj))
    44. })
    45. // delete 删除
    46. app.delete('/user/:id', (req, res) => {
    47. const obj = {
    48. id: req.params.id
    49. }
    50. console.log(obj);
    51. res.writeHead(200, { 'Content-Type': 'text/plain;charset=utf-8' })
    52. res.end(`成功删除了${req.params.id}的数据`)
    53. })
    54. app.listen(8888)

    客户端

    get请求

    1. // get
    2. sendGet(){
    3. axios.get('http://localhost:8080/api1/user/1').then(res=>//接收数据
    4. console.log(res.data);
    5. }).catch(err=>{ //捕获异常
    6. console.log(err);
    7. })
    8. },

    post

    1. // post
    2. sendPost(){
    3. axios.post('http://localhost:8080/api1/user',{
    4. first_name:"张",
    5. last_name:"三"
    6. }).then(res=>//接收数据
    7. console.log(res.data);
    8. }).catch(err=>{ //捕获异常
    9. console.log(err);
    10. })
    11. },

    put

    1. // put
    2. sendPut(){
    3. axios.put('http://localhost:8080/api1/user/:id',{
    4. first_name:"老",
    5. last_name:"六"
    6. }).then(res=>//接收数据
    7. console.log(res.data);
    8. }).catch(err=>{ //捕获异常
    9. console.log(err);
    10. })
    11. },

    delete

    1. // delete
    2. sendDelete(){
    3. axios.delete('http://localhost:8080/api1/user/1').then(res=>//接收数据
    4. console.log(res.data);
    5. }).catch(err=>{ //捕获异常
    6. console.log(err);
    7. })
    8. },

     和并发送

    1. // 和并发送
    2. sendArr(){
    3. const get1 = axios.get('http://localhost:8080/api1/hello')
    4. const get2 = axios.get('http://localhost:8080/api2/hello')
    5. axios.all([get1,get2]).then(
    6. axios.spread((res1,res2)=>{
    7. console.log(res1.data,res2.data);
    8. })
    9. ).catch(err=>{
    10. console.log(err);
    11. })
    12. },

    基本的axios请求

    1. sendApi(){
    2. axios.defaults.baseURL='http://localhost:8080'
    3. axios({
    4. url:"/api1/user",
    5. // baseURL:"http://localhost:8080",
    6. method:"post",
    7. data:{
    8. first_name:"张",
    9. last_name:"三"
    10. }
    11. }).then(res=>//接收数据
    12. console.log(res.data);
    13. }).catch(err=>{ //捕获异常
    14. console.log(err);
    15. })
    16. },

     timeout :设置超时时间   端口请求时间不可超出时间

    1. sendApitwo(){
    2. const instance = axios.create({
    3. baseURL:"http://localhost:8080",
    4. timeout:1000
    5. });
    6. instance.get('/api1/user/1').then(res=>{
    7. console.log(res.data);
    8. }).catch(err=>{
    9. console.log(err);
    10. })
    11. }
  • 相关阅读:
    第5/100天 阅读笔记
    价值1000的情感爆文写作prompt,助你写出10万+阅读微信爆文
    什么是N卡和A卡?有什么区别?
    FFmpeg常用命令行讲解及实战一
    苹果手机iOS18最新升级:植入AI人工智能,国内百度文心一言,国外GPT4o来辅助
    循环链表3
    RTSP/Onvif安防视频平台EasyNVR级联至EasyNVS系统不显示通道,是什么原因?
    asp.net core、c#关于路径的总结
    INTERSPEECH 2022|FS-CANet: 基于全带子带交叉注意力机制的语音增强
    程序分析与优化 - 11 多分支分析
  • 原文地址:https://blog.csdn.net/red_HTML/article/details/127368826