spring boot 学习笔记(005)提交json对象

来源:互联网 发布:上海网页美工培训班 编辑:程序博客网 时间:2024/04/30 08:35

提交post对象应该是很简单的,但是掉ajax的坑里去了。


1,首先,HelloWorld.Java 代码中加入:


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @RequestMapping(value="/trequest", method = RequestMethod.POST)    
  2. @ResponseBody  
  3.    public UserInfo trequest(@RequestBody UserInfo pu){  
  4.   
  5.     UserInfo u = new UserInfo();  
  6.     u.setUserCode(pu.getUserCode());  
  7.     u.setUserName(pu.getUserName());  
  8.     u.setDeptCode(pu.getDeptCode());  
  9.   
  10.     return u;  
  11.    }  

2,在static目录下,新建index.html文件:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html>  
  3.   <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  6.     <title>json调试器</title>  
  7.     <link rel="stylesheet" href="jquery/jquery.mobile-1.3.2.min.css">  
  8.     <script src="jquery/jquery-1.8.3.min.js"></script>  
  9.     <script src="jquery/jquery.mobile-1.3.2.min.js"></script>  
  10.   </head>  
  11.   
  12.   <body>  
  13.   <div data-role="page">  
  14.     <div data-role="header">  
  15.       <h1>json调试器</h1>  
  16.     </div>  
  17.   <div data-role="content">  
  18.     <form method="post" name="qform">  
  19.       <label for="qname">请输入URL:</label>  
  20.       <input type="text" name="urltext" id="urltext" value="http://localhost:8090/trequest" />  
  21.       <label for="qname">请输入Request内容:</label>  
  22. <textarea  id="requesttext"></textarea>  
  23.       <input type="button" id="btnPostJson" data-inline="true" onclick="postJson();" value="提交" />  
  24.     </form>  
  25.     <label id="responsttxt"></label>  
  26.   </div>  
  27.     <div data-role="footer">  
  28.       <h4 id="foottext"></h4>  
  29.     </div>  
  30.   </div>  
  31.   </body>  
  32.     
  33.   <script>  
  34.       
  35.   console.log("come in!");  
  36.   
  37.   function postJson() {  
  38.       
  39.     console.log(document.getElementById('requesttext').value);  
  40.   
  41.     $.ajax({   
  42.         type : "POST",   
  43.         url  : qform.urltext.value,    
  44.         cache : false,   
  45.         data : document.getElementById('requesttext').value,   
  46.         success : onSuccessResult,   
  47.         error : onErrorResult  
  48.     });  
  49.       
  50.     return false;   
  51.   }  
  52.   
  53.   function onSuccessResult(data,status){  
  54.     console.log(data);  
  55.     var jsondata = JSON.stringify(data);  
  56.     console.log(jsondata);  
  57.       
  58.     showText(jsondata);  
  59.   }  
  60.   
  61.   function onErrorResult(xmlhttprequest, textstatus, errorthrown){   
  62.     //进行错误处理  
  63.     showText(errorthrown);  
  64.   }  
  65.     
  66.   function showText(txt) {  
  67.     var showingtxt = document.getElementById("responsttxt");  
  68.     showingtxt.innerHTML = '<label id="responsttxt">' + txt + '</label>';  
  69.   }  
  70.   
  71.   </script>  
  72. </html>  

 3,这里,前端用了jquery,需要在static目录下,建立子目录:jquery,

然后上传几个jQuery文件。文件打包下载:

http://download.csdn.net/detail/yihui823/9562419


4,进入页面,点击,报错:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. {"timestamp":1467133626700,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","path":"/trequest"}  


5,原来,ajax做post的时候,默认是
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Content type 'application/x-www-form-urlencoded;charset=UTF-8'  

在“$.ajax({ ”之前,需要加上:

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. $.ajaxSetup({    
  2.        contentType : 'application/json'    
  3.    });  

然后访问:

http://localhost:8090/


request内容填入:

{"userCode":"h002","userName":"u name"}


可以得到结果:

{"userCode":"h002","userName":"u name","deptCode":null}


如图:



搞定,打完收功!


0 0