VUE_AXIOS.js

axios說(shuō)明文檔:
https://www.kancloud.cn/yunye/axios/234845

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>復(fù)習(xí)axios </title>
        <!-- vue核心 -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <!-- 引入axios -->
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    </head>
    <body>
        <div id="app">
            <button type="button" @click="sendget">AjaxGet方式</button>
            <button type="button" @click="sendget2">AjaxGet2。submit方式</button>
        </div>
        <script>
            var vm = new Vue({
                el: "#app",
                data: {

                },
                /* 函數(shù) */
                methods: {
                    /* 出發(fā)ajax觸發(fā)的函數(shù) */
                    sendget() {
                        //springboot controller 層的路徑
                        axios.get('http://localhost:8878/springboot/test?id=20&name=張老飛')
                            /* 成功后執(zhí)行 */
                            .then(function(response) {
                                console.log(response);
                                console.log(response.data);
                            })
                            .catch(function(error) {
                                //失敗了,
                                console.log(error);
                            });
                    },

                    sendget2() {
                        //springboot controller 層的路徑
                        axios.get('http://localhost:8878/springboot/submit?no=20')
                            /* 成功后執(zhí)行 */
                            .then(function(response) {
                                console.log(response);
                                console.log(response.data);
                            })
                            .catch(function(error) {
                                //失敗了,
                                console.log(error);
                            });
                    }
                },
            })
        </script>
    </body>
</html>

再sprintboot項(xiàng)目中配置跨域的請(qǐng)求
@CrossOrigin(origins="*",maxAge=3600)

后臺(tái)

package com.neuedu.springboot.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.springboot.model.bean.Hero_User;
import com.neuedu.springboot.model.service.Hero_UserService;


@Controller
@CrossOrigin(origins="*",maxAge=3600)
//* 所有請(qǐng)求都跨域
//maxAge最大時(shí)間
public class Hero_UserController {
    //注入service的依賴
    @Autowired
    private Hero_UserService serivce;
    
    //@RequestMapping(value="submit" ,method=RequestMethod.POST)
    @RequestMapping("submit")
    @ResponseBody//返回jsion字符串
    public String selectAll(HttpSession session,int no) {
        
        System.out.println("submit");
        Hero_User slectno =new Hero_User();
        
        slectno=serivce.getHero_UserNO(no);
        System.out.println(slectno);
        
//      List<Hero_User> list=new ArrayList<>();
//      list=serivce.getHreo_User();
//      for (Hero_User hero_User : list) {
//          System.out.println(hero_User);
//      }
//      session.setAttribute("list", list);
        
        return "{\"result\":\"yes\"}";
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容