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>