2019-04-22 | Ajax | UNLOCK

Spring Boot - Consuming RESTful Web Service


通过jquery的ajax封装测试使用RESTful API,代码如下;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Consume RESTful Web Service</title>
</head>
<body>

<div id = "productsJson"></div>

<hr/>
<button>Click here to submit the form</button>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script><script>
$(document).ready(function () {
$.getJSON("http://localhost:8080/products",function (result) {
$.each(result,function(key,value){
$("#productsJson").append(value.id+" "+value.name+" ")
})
})
},
$("button").click(function () {
var productmodel = {
id: "3",
name:"Ginger"
};
var requestJSON = JSON.stringify(productmodel);
$.ajax({
type:"POST",
url:"http://localhost:8080/products",
headers:{
"Content-Type":"application/json"
},
data:requestJSON,
success:function (data) {
alert(data);
},
error:function (data) {

}
})
})

)
</script>

</body>
</html>

测试

评论加载中