Skip to content

Commit a19d12a

Browse files
committed
receive json str/json object
1 parent d56f5b0 commit a19d12a

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

src/main/java/cn/com/ttblog/ssmbootstrap_table/controller/JsonpController.java

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
import javax.servlet.http.HttpSession;
1212

1313
import org.apache.commons.lang3.builder.ToStringBuilder;
14+
import org.eclipse.jetty.util.log.Log;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
1617
import org.springframework.stereotype.Controller;
1718
import org.springframework.ui.Model;
1819
import org.springframework.web.bind.annotation.ModelAttribute;
1920
import org.springframework.web.bind.annotation.PathVariable;
21+
import org.springframework.web.bind.annotation.RequestBody;
2022
import org.springframework.web.bind.annotation.RequestMapping;
2123
import org.springframework.web.bind.annotation.RequestMethod;
2224
import org.springframework.web.bind.annotation.RequestParam;
@@ -176,10 +178,19 @@ public User t(@PathVariable Long id, HttpServletRequest request,
176178

177179
/**
178180
* http://localhost:8080/ssmbootstrap_table/jsonp/testarr?v[]=1&v[]=2&v[]=3
179-
* var arr=["a","b","c"]; 前端jquery发送数组,需要traditional置为 true $.ajax({
180-
* url:"http://localhost:8080/ssmbootstrap_table/jsonp/testarr",
181-
* data:{"v":arr}, type:"post", traditional: true, success:function(data){
182-
* console.log(data); }, error:function(xhr){ console.log(xhr); } });
181+
* var arr=["a","b","c"]; 前端jquery发送数组,需要traditional置为 true
182+
* $.ajax({
183+
* url:"http://localhost:8080/ssmbootstrap_table/jsonp/testarr",
184+
* data:{"v":arr},
185+
* type:"post",
186+
* traditional: true,
187+
* success:function(data){
188+
* console.log(data);
189+
* },
190+
* error:function(xhr){
191+
* console.log(xhr);
192+
* }
193+
* });
183194
*
184195
* @param values
185196
* @return
@@ -190,7 +201,57 @@ public User t(@PathVariable Long id, HttpServletRequest request,
190201
logger.debug("接收到的数组参数:{}", Arrays.deepToString(values));
191202
return values;
192203
}
204+
205+
/**
206+
var d={"id":1,name:"test"};
207+
$.ajax({
208+
url:"http://localhost:8080/ssmbootstrap_table/jsonp/receivejson",
209+
data:{"json":JSON.stringify(d)},
210+
type:"post",
211+
ContentType: "application/json;charset=UTF-8",
212+
success:function(data){
213+
console.log(data);
214+
},
215+
error:function(xhr){
216+
console.log(xhr);
217+
}
218+
});
219+
get to http://localhost:8080/ssmbootstrap_table/jsonp/receivejson?json={%22id%22:1,name:%22test%22}
220+
*
221+
* @param values
222+
* @return
223+
*/
224+
@RequestMapping("/receivejson")
225+
public @ResponseBody Object receivejson(@RequestParam(value="json") String json) {
226+
logger.debug("receive json str:{}",json);
227+
JSONObject j=(JSONObject) JSONObject.parse(json);
228+
logger.debug("receive json to jsonobject:{}",j);
229+
return j;
230+
}
193231

232+
/**
233+
$.ajax({
234+
url:"http://localhost:8080/ssmbootstrap_table/jsonp/receivejsonobj",
235+
type:"post",
236+
contentType:'application/json;charset=UTF-8',
237+
data: "{id:1,name:\"test\"}",
238+
success:function(data){
239+
console.log(data);
240+
},
241+
error:function(xhr){
242+
console.log(xhr);
243+
}
244+
});
245+
*
246+
* @param values
247+
* @return
248+
*/
249+
@RequestMapping("/receivejsonobj")
250+
public @ResponseBody User receivejsonobj(@RequestBody User user) {
251+
logger.debug("receive json obj:{}",user);
252+
return user;
253+
}
254+
194255
/**
195256
* 获取session中name的值
196257
*

0 commit comments

Comments
 (0)