简体   繁体   中英

how to test a json response from spring Mvc controller with ajax?

i'm trying to test an ajax code that retreives json response from controller

this is my spring controller

 @RequestMapping(value = "/json", method = RequestMethod.GET)
 public @ResponseBody List<User> jsonresponse(Model model) {
 model.addAttribute("user", new User());
 List<User> l= new ArrayList<User>();
   l=this.userService.getUserList();
 return l;
 }

this is what the url /json in the browser shows : (data is retreived from database)

 [{"time":"11:01:00","val1":123,"val2":124,"val3":11,"val4":140,"val5":100},

{"time":"11:11:00","val1":140,"val2":100,"val3":13,"val4":100,"val5":120}]

I want to test an ajax code that extracts data from json and display it in the jsp page . I've tried this code but nothing is shown !

$.ajax({
type: "GET",
contentType : 'application/json; charset=utf-8',
dataType : 'JSON',
url: "/json",
data: ,
success :function(json) {
alert( json.time , json.val1) ;

}
});

I'm new with ajax and json . If there are some mistakes please tell me.

You have a syntax error in your code, remove the line:

data: ,

To avoid these kind of errors open the developer console in your browser and check if it shows any errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM