首页 > 其他分享 >vue 发起get请求和post请求

vue 发起get请求和post请求

时间:2024-06-09 15:43:40浏览次数:14  
标签:npm vue 请求 get project his post data

一、vite方式初始化vue3项目

C:\Users\Administrator>npm init vite-app his-project

> npx
> create-vite-app his-project

Scaffolding project in C:\Users\Administrator\his-project...

Done. Now run:

  cd his-project
  npm install (or `yarn`)
  npm run dev (or `yarn dev`)


C:\Users\Administrator>cd his-project

C:\Users\Administrator\his-project>npm install
npm warn deprecated [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm warn deprecated [email protected]: Glob versions prior to v9 are no longer supported
npm warn deprecated [email protected]: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-terser
npm warn deprecated [email protected]: Please use @jridgewell/sourcemap-codec instead

added 299 packages, and audited 300 packages in 1m

39 packages are looking for funding
  run `npm fund` for details

7 vulnerabilities (1 low, 3 moderate, 3 high)

To address issues that do not require attention, run:
  npm audit fix

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.

C:\Users\Administrator\his-project>npm run dev

> [email protected] dev
> vite

[vite] Optimizable dependencies detected:
vue

  Dev server running at:
  > Network:  http://10.111.40.30:3000/
  > Network:  http://192.168.10.225:3000/
  > Local:    http://localhost:3000/

 

二、vue3

修改his-project\src\components\HelloWorld.vue文件,支持发起get请求和post请求

<template>  
  <div>  
    <button @click="fetchData">发起 GET 请求</button>  
    <button @click="postData">发起 POST 请求</button>  
    <!-- 你可以在这里添加用于显示响应数据的元素 -->  
    <div v-if="responseData">  
      <pre>{{ responseData }}</pre>  
    </div>  
  </div>  
</template>  
  
<script>  
import axios from 'axios';  
  
export default {  
  data() {  
    return {  
      responseData: null, // 用于存储响应数据的变量  
    };  
  },  
  methods: {  
    // 发起 GET 请求的方法  
    fetchData() {  
      axios.get('http://127.0.0.1:5000/get_data') // 替换为你的 API URL  
        .then(response => {  
          this.responseData = response.data; // 存储响应数据  
        })  
        .catch(error => {  
          console.error('GET 请求失败:', error);  
        });  
    },  
    // 发起 POST 请求的方法  
    postData() {  
      const postData = {  
        key1: 'value1',  
        message: 'value2' // 根据你的 API 要求设置数据  
      };  
  
      axios.post('http://127.0.0.1:5000/post_data', postData) // 替换为你的 API URL  
        .then(response => {  
          this.responseData = response.data; // 存储响应数据  
        })  
        .catch(error => {  
          console.error('POST 请求失败:', error);  
        });  
    },  
  },  
};  
</script>

 

 

 

三、后端flask配置get请求接口和post请求接口

from flask import Flask, request, jsonify  
from flask_cors import CORS
  
app = Flask(__name__)  
CORS(app, resources=r'/*')
@app.route('/get_data', methods=['GET'])  
def get_data():  
    # 这里可以添加获取数据的逻辑  
    data = {'message': 'Hello from GET request','data':'test_data'}  
    return jsonify(data)  


  
@app.route('/post_data', methods=['POST'])  
def post_data():  
    # 假设我们期望收到一个 JSON 数据体  
    data = request.get_json()  # 从请求中获取 JSON 数据  
    # 你可以在这里添加处理数据的逻辑  
    message = data.get('message', 'No message provided')  
    response = {'message': f'Received: {message}'}  
    return jsonify(response), 201  # 201 是CREATED状态码,表示资源已成功创建  
  
if __name__ == '__main__':  
    app.run(debug=True)

 

四、页面效果,点击按钮发起请求,显示接口返回信息

 

标签:npm,vue,请求,get,project,his,post,data
From: https://www.cnblogs.com/superbaby11/p/18239620

相关文章

  • postgresql 基本查询
    建表语句--========sys_dict_typecreatetablesys_dict_type(idbigintprimarykey,namevarchar(100),typevarchar(100),group_codevarchar(100),statuschar(1));commentontablesys_dict_typeis'系统字典类型表';commentoncolumnsys_dict_type.nam......
  • postgresql 数据库基本管理
    逻辑结构PostgreSQL教程--逻辑结构:实例、数据库、schema、表之间的关系数据库基本管理--查询所有数据库selectdatnamefrompg_catalog.pg_database;--创建数据库createdatabasejxwithencoding'UTF8'LC_COLLATE='C'LC_CTYPE='C'TEMPLATE=template1;--查询......
  • Java项目-基于springboot+vue的音乐网站与分享平台 (源码+数据库+文档)​
    如需完整项目,请私信博主基于SpringBoot+Vue的音乐网站与分享平台开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven音乐网站与分享平台的主要使用者分为管理员和用户,实现功能包括管理员:首页、个人中心、用户管理、音乐资讯管理、音乐......
  • Java项目-基于springboot+vue的影城管理系统 (源码+数据库+文档)​
    如需完整项目,请私信博主基于SpringBoot+Vue的影城管理系统开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven影城管理系统的主要使用者分为管理员和用户,实现功能包括管理员:首页、个人中心、用户管理、电影类型管理、放映厅管理、电影......
  • 基于SpringBoot+Vue的学院党员管理系统设计与实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示项目运行截图技术框架后端采用SpringBoot框架前端框架Vue可行性分析系统测试系统测试的目的系统功能测试数据库表设计代码参考数据库脚本为什么选择我?获取源码前言......
  • 基于SpringBoot+Vue的小型企业办公自动化系统设计与实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示项目运行截图技术框架后端采用SpringBoot框架前端框架Vue可行性分析系统测试系统测试的目的系统功能测试数据库表设计代码参考数据库脚本为什么选择我?获取源码前言......
  • 基于 Vue 与 SpringBoot 框架的学生成绩分析 和弱项辅助系统设计
    摘   要针对相关学科考试中遇到的学生弱项管理情况,设计了一套基于 B/S 架构的学生弱项辅助系统。系统采用前后端分离开发,前端使用VUE 框架,后端使用SpringBoot框架实现,使用mysql数据库来进行数据持久层的搭建。前后端通过 Axios库进行数据交互。系统在数据管理和......
  • Vue3中的常见组件通信之`$refs`、`$parent`
    Vue3中的常见组件通信之$refs、$parent概述​在vue3中常见的组件通信有props、mitt、v-model、refs、......
  • 关于Vue开发中的网页路由
    引言Vue.js是一个用于构建用户界面的渐进式JavaScript框架。它设计得非常灵活,允许你以不同的方式将其集成到你的项目中,从简单的交互式页面到复杂的单页应用程序(SPA)。Vue.js的核心库只关注视图层,这使得它非常容易学习,并且与其他库或现有项目集成VueRoute是什么?VueRouter......
  • SpringBoot+Vue月度员工绩效考核管理系统(前后端分离)
    技术栈JavaSpringBootShrioMavenMySQLMyBatisVueElement-UI角色对应功能管理员登录修改密码岗位管理部门管理绩效指标管理员工管理绩效考核管理公告信息管理用户登录个人信息修改修改密码绩效考核指标浏览绩效考核浏览功能截图......