响应json数据之响应json格式数据
生活随笔
收集整理的這篇文章主要介紹了
响应json数据之响应json格式数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title><script src="js/jquery.min.js"></script><script>// 頁面加載,綁定單擊事件$(function(){$("#btn").click(function(){// alert("hello btn");// 發送ajax請求$.ajax({// 編寫json格式,設置屬性和值url:"user/testAjax",contentType:"application/json;charset=UTF-8",data:'{"username":"hehe","password":"123","age":30}',dataType:"json",type:"post",success:function(data){// data服務器端響應的json的數據,進行解析alert(data);alert(data.username);alert(data.password);alert(data.age);}});});});</script></head>
<body><button id="btn">發送ajax的請求</button></body>
</html>
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.9.0</version></dependency>
package com.learn.controller;import com.learn.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@Controller
@RequestMapping("/user")
public class UserController {/*** 模擬異步請求響應*/@RequestMapping("/testAjax")public @ResponseBody User testAjax(@RequestBody User user){System.out.println("testAjax方法執行了...");// 客戶端發送ajax的請求,傳的是json字符串,后端把json字符串封裝到user對象中System.out.println(user);// 做響應,模擬查詢數據庫user.setUsername("haha");user.setAge(40);// 做響應return user;}}
?
總結
以上是生活随笔為你收集整理的响应json数据之响应json格式数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 响应json数据之过滤静态资源
- 下一篇: 文件上传之传统方式上传代码回顾