<!DOCTYPE html>
<html>
<head>
	<title>jquery</title>
	<script src="https://code.jquery/jquery-3.3.1.min.js"></script>
</head>
<body>
	
<button onclick="sendAjaxGet()">发送请求GET</button>
<button onclick="sendAjaxPost()">发送请求POST</button>

<script type="text/javascript">
	function sendAjaxPost(){
		let json = {
			username: "lindont",
			password: "123456"
		}
		$.ajax({
           type: "POST",
           url: "http://localhost:8787/test/params",
           contentType: "application/json; charset=utf-8",
           data: JSON.stringify(json), /*传给后端的数据格式json*/
           dataType: "json",	/*后端返回的数据格式json*/
           success: function(data){
              console.log(data);
           },
           error: function (message) {

           }
        });
	}


	function sendAjaxGet() {
		$.ajax({
           type: "GET",
           url: "http://localhost:8787/test",
           success: function(data){
              console.log(data);
           },
           error: function (message) {
            
           }
        });
	}
</script>
</body>
</html>

 

更多推荐

Ajax请求后端数据