溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

axios和SpringBoot前端怎么調(diào)用后端接口進(jìn)行數(shù)據(jù)交互

發(fā)布時(shí)間:2023-03-16 13:39:24 來源:億速云 閱讀:102 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“axios和SpringBoot前端怎么調(diào)用后端接口進(jìn)行數(shù)據(jù)交互”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“axios和SpringBoot前端怎么調(diào)用后端接口進(jìn)行數(shù)據(jù)交互”文章能幫助大家解決問題。

一、介紹

一個(gè)完善的系統(tǒng),前后端交互是必不可少的,這個(gè)過程可以分成下面幾步:

前端向后端發(fā)起請(qǐng)求后端接口接收前端的參數(shù)后,開始層層調(diào)用方法處理數(shù)據(jù)后端將最終數(shù)據(jù)返回給前端接口前端請(qǐng)求成功后,將數(shù)據(jù)渲染至界面

二、項(xiàng)目結(jié)構(gòu)

前端技術(shù):axios

后端技術(shù):SpringBoot(這個(gè)也無(wú)所謂,但是你一定要有控制層的訪問路徑,也就是所謂的請(qǐng)求地址對(duì)應(yīng)的方法,可以用SSM框架,SSH框架,都可以)

axios和SpringBoot前端怎么調(diào)用后端接口進(jìn)行數(shù)據(jù)交互

上面是大致的文件結(jié)構(gòu),相信大家后端的數(shù)據(jù)處理都沒問題,無(wú)非就是:

  • 控制層接收前端請(qǐng)求,調(diào)用對(duì)應(yīng)的業(yè)務(wù)層接口方法

  • 業(yè)務(wù)層實(shí)現(xiàn)類去實(shí)現(xiàn)業(yè)務(wù)層接口

  • 業(yè)務(wù)層實(shí)現(xiàn)類的方法內(nèi)調(diào)用數(shù)據(jù)層的接口

  • 數(shù)據(jù)層實(shí)現(xiàn)文件(mapper.xml)實(shí)現(xiàn)數(shù)據(jù)層接口

  • 然后處理結(jié)果層層返回

三、代碼編寫

我們只介紹前端界面+控制層,首先是前端界面

第一步:引入相關(guān)文件

axios和SpringBoot前端怎么調(diào)用后端接口進(jìn)行數(shù)據(jù)交互

這里的axios就是我們發(fā)起請(qǐng)求所必備的文件,這些文件在文章末尾會(huì)有給出。

前端代碼如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>測(cè)試</title>
    <script src="../static/js/jquery.min.js"></script>
    <script src="../static/js/axios.min.js"></script>
</head>
<body>
<span id="text">我是前端默認(rèn)值</span>
<script>
    window.onload =function() {  //一加載界面就調(diào)用
        $.ajax({url:"testTest?num=1",success:function(result){
                document.getElementById("text").innerHTML=result;
            }});
    };
</script>
</body>
</html>

后端控制層代碼如下:

    @RequestMapping("/testTest")  //控制層
    @ResponseBody
    public int testTest(int num) {
        if(num==1) return 1;
        if(num==2) return 2;
        return 0;
    }

很明顯,大家看看應(yīng)該就明白了,前端發(fā)請(qǐng)求時(shí)可以攜帶數(shù)據(jù),比如賬號(hào)、密碼啊等等,后端接收后,就可以處理啦,然后把處理結(jié)果返回給前端,

前端接收后,就可以渲染了,或者給出操作成功的提示。

效果:

axios和SpringBoot前端怎么調(diào)用后端接口進(jìn)行數(shù)據(jù)交互

四、運(yùn)用

1、字符串、整形等(新增功能)

前端代碼:

 <el-dialog title="創(chuàng)建車輛裝備" :visible.sync="insertVisible" width="30%">
        <el-form :model="equipment" ref="equipment" label-width="100px" class="demo-ruleForm">
            <el-form-item label="名稱" prop="name">
                <el-input v-model="equipment.name"></el-input>
            </el-form-item>
            <el-form-item label="類型" prop="type">
                <el-input v-model="equipment.type"></el-input>
            </el-form-item>
            <el-form-item label="庫(kù)存數(shù)量" prop="inventory">
                <el-input type="number" v-model="equipment.inventory"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="insertVisible = false">取 消</el-button>
                <el-button type="primary" @click="insertEquipment" data-toggle="modal" data-target="#myModal">確 定</el-button>
              </span>
    </el-dialog>
<script type="text/javascript">
    new Vue({
        el:"#box",
        data:{
            id:"",			//裝備主鍵
            equipment:{},				//一條equipment數(shù)據(jù)
            insertVisible:false //新增提示框控制器:true顯示/false隱藏
        },
        methods:{
            //打開新增提示框
            openInsertPanel:function(){
                this.insertVisible = true;
                this.equipment = {};
            },
            //創(chuàng)建equipment
            insertEquipment:function(){
                var name = this.equipment.name;
                var type = this.equipment.type;
                var inventory = this.equipment.inventory;
                var that = this;
                axios.put("insertEquipment?name="+name+"&type="+type+"&inventory="+inventory).then(function(result){
                    if(result.data.status){
                        that.selectAllEquipment();
                        that.insertVisible = false;
                    }else{
                        that.$message.error(result.data.message);
                        that.insertVisible = false;
                    }

                });
            },
        }
    });
</script>

后端代碼

    @RequestMapping("/insertEquipment")  //增加裝備
    @ResponseBody
    public ResultMap insertEquipment(String name, String type,String inventory) {
        try {
            int realInventory=Integer.valueOf(inventory);
            Equipment equipment=new Equipment(name,type,realInventory);
            equipmentService.insertEquipment(equipment);
            resultMap.setStatus(true);
        } catch (Exception e) {
            resultMap.setStatus(false);
            resultMap.setMessage(e.getMessage());
        }
        return resultMap;
    }

關(guān)于“axios和SpringBoot前端怎么調(diào)用后端接口進(jìn)行數(shù)據(jù)交互”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI