Springboot与Ajax整合练习?

Springboot与Ajax整合练习?
RunApp
package cn.tedu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RunApp {
    public static void main(String[] args) {
        SpringApplication.run(RunApp.class);
    }
}

练习类
package cn.tedu.pojo;

public class Student {
    private String name;
    private String subject;
    private Integer id;
    private Double score;
    private Double height;

    public Student(String name, String subject, Integer id, Double score, Double height) {
        this.name = name;
        this.subject = subject;
        this.id = id;
        this.score = score;
        this.height = height;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", subject='" + subject + '\'' +
                ", id=" + id +
                ", score=" + score +
                ", height=" + height +
                '}';
    }
}

控制类
package cn.tedu.controller;

import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("st")
@CrossOrigin
public class StuController {
    @RequestMapping("get")
    public Student get(){
        Student s =new Student("Melt","Killer-Queen",193,100.55,133.5);
        System.out.println(s);
        return s;

    }
    @RequestMapping("get2")
    public Student get2(Student st){
//        Student s =new Student("Melt","Killer-Queen",193,100.55,133.5);
        System.out.println(st);
        return st;
    }
}

HTML
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Meltry</title>
		<script src="jquery-1.8.3.min.js"></script>
		<script>
			function a(){
				$.ajax({
					type:"get",
					url:"http://localhost:8080/st/get",
					success:function(data){
						console.log(data);
						console.log(data.name);
						alert(50);
					},
					error:function(data){
						alert(25);
					}
				})
			}
			function b(){
				$.ajax({
					type:"get",
					url:"http://localhost:8080/st/get2",
					data:{
						"name":"BB",
						"subject":"护士",
						"id":626,
						"score":555.5,
						"height":123.3
					},
					success:function(data){
						console.log(data);
						console.log(data.name);
						alert(50);
					},
					error:function(data){
						alert(25);
					}
				})
			}
		</script>
	</head>
	<body>
		<a onclick="a();"href="#">click here please</a>
		<a onclick="b();"href="#">click here please</a>
	</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值