简体   繁体   中英

jQuery.get from Spring RestController

How can should I send the data from Spring controller to the client? Do I need to wrap it as Json or is there an easier way?

(noob in web. So, please bear with me)

@RequestMapping("/abc")
@RestController
public class ListController {

@RequestMapping(value = "/d", method = RequestMethod.GET)
public StringOrJson? getData() {
    return "myData";
}

On the client:

function checkBoxToggled(){
    $(document).get('abc/d', function( data ) {
      alert('Data Loaded2:' + data );
    }); 
}

It will be great if you declare the @RequestMapping as follows:

 @RequestMapping(value = "/d", method = RequestMethod.GET, produces = "application/json")

You can return simple String response.

Or you can always reutrn JSONObject as follows:

return new JSONObject("{'data':'myData'}");

In that case the return value in the method signature should be replaced to JSONObject .

You just need to modify your Controller to:

@RequestMapping("/abc")
@Controller
public class ListController {
    @RequestMapping(value = "/d", method = RequestMethod.GET)
    public String getData() {
        return "myData";
    }
}

And the client should be:

jQuery.get("abc/d", function (data) {
    alert('Data Loaded2:' + data );
});  

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