RESTful风格实战(ajax+RESTful+button实现交互)

欢迎页面有四个按钮分别实现get、post、put、delete四种传输方式
并返回响应消息

程序中用的ajax来改变传输方式
在controller类中响应

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎页</title>
    <script src="webjars/jquery/3.6.0/dist/jquery.min.js"></script>

    <script>
        function get(){
            $.ajax({
                url:"/Index/RESTful",
                type:"get",
                success:function(text){
                    window.alert(text);
                }
            })
        }

        function post(){
            $.ajax({
                url:"/Index/RESTful",
                type:"post",
                success:function(text){
                    window.alert(text);
                }
            })
        }


        function put(){
            $.ajax({
                url:"/Index/RESTful",
                type:"put",
                success:function(text){
                    window.alert(text);
                }
            })
        }

        function delet(){
            $.ajax({
                url:"/Index/RESTful",
                type:"delete",
                success:function(text){
                    window.alert(text);
                }
            })
        }

    </script>
</head>
<body>
    <h1>欢迎页</h1>
        <button id="get" onclick="get()">GET</button>
        <button id="delete" onclick="delet()">DELETE</button>
        <button id="put"  onclick="put()">PUT</button>
        <button id="post" onclick="post()">POST</button>
</body>
</html>

indexController

package com.igeek.controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Index")
public class IndexController {

    @GetMapping("/RESTful")
    public String getRESTful(){
        return "点击了Get";
    }

    @DeleteMapping("/RESTful")
    public String deleteRESTful(){
        return "点击了delete";
    }
    @PutMapping("/RESTful")
    public String putRESTful(){
        return "点击了put";
    }
    @PostMapping("/RESTful")
    public String postRESTful(){
        return "点击了post";
    }
}

效果图

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值