简体   繁体   English

将模型对象从控制器返回到 Ajax jquery 在 Spring mvc 中不起作用

[英]Returning model object from Controller to Ajax jquery not working in Spring mvc

I am trying to return a model object from spring mvc controller back to jquery ajax method but it is returning blank as the response我正在尝试将模型对象从 spring mvc 控制器返回到 jquery ajax 方法,但它返回空白作为响应

jsp: jsp:

$( "#dialog-link10" ).click(function( event ) {
var appname= $("#dialog-link10").text();
alert(appname);
if(appname == 'RS Applications') {
$.ajax({
    type : "GET",
    url : 'abc.html',
    dataType: 'JSON' ,
    data: 
        {"id" : $("#dialog-link10").text()}
    ,
    success : function(data) {
        alert('success')
        alert('data')

    }
});}

controller:控制器:

@RequestMapping(method=RequestMethod.GET, value="/abc")
@ResponseBody
public  Model helloWorld2( @RequestParam("id") String id, Model model) {

    System.out.println("*****"+id);

    List <String> list1=new ArrayList<String>();
    List <String> list2=new ArrayList<String>();

        System.out.println("here");

        list1.add("abc");
        list1.add("abc2");
        list1.add("abc3");
        list1.add("abc4");

        model.addAttribute("list1", list1);

        return model;
        }

This is not generating success alert as well.这也不会产生成功警报。 Please suggest请建议

Your method should return directly the list, as a json, no need to put it in the model and return the model.您的方法应该直接返回列表,作为 json,无需将其放入模型中并返回模型。 Also check if you have an error in your callback ajax.还要检查您的回调 ajax 中是否有错误。

You are specifying dataType: 'JSON' in your ajax call, but you are not converting your response object (model) to json in your controller.您在 ajax 调用中指定dataType: 'JSON' ,但您没有在控制器中将响应对象(模型)转换为 json 。

From the jQuery ajax documentation for dataType setting:来自dataType设置的 jQuery ajax 文档

The type of data that you're expecting back from the server.您期望从服务器返回的数据类型。 If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string)如果没有指定,jQuery 将尝试根据响应的 MIME 类型推断它(XML MIME 类型将产生 XML,在 1.4 中 JSON 将产生一个 JavaScript 对象,在 1.4 中脚本将执行脚本,其他任何东西都会作为字符串返回)

You'll probably want to reference a json serializer (unless you want to write your own) - but the important part is serializing your model to json in your response.您可能想要引用一个 json 序列化程序(除非您想编写自己的序列化程序)——但重要的部分是将您的model序列化为响应中的 json。 For example (using json-io ):例如(使用json-io ):

String jsonModel = JsonWriter.objectToJson(model);
return jsonModel;

You can then access the string array contained in your json response object in the following way:然后,您可以通过以下方式访问 json 响应对象中包含的字符串数组:

success : function(data) {
     for (i = 0; i < data.list1.length; i++){
          alert(data.list1[i]);
     }
}

And a fiddle example for reading json objects还有一个用于读取 json 对象的小提琴示例

I met this problem and it took me several hours to find out the problem.我遇到了这个问题,我花了几个小时才发现问题。
Just remove "Model model" from your parameter.只需从您的参数中删除“模型模型”。
Use a Map<String, String> or Map<String, Object> instead.请改用Map<String, String>Map<String, Object>

Try adding the produces = MediaType.APPLICATION_JSON to the RequestMapping annotation尝试将produces = MediaType.APPLICATION_JSON添加到 RequestMapping 注释中

@RequestMapping(method=RequestMethod.GET, value="/abc", produces = MediaType.APPLICATION_JSON)

Also, you may not need the Model paramater in the method.此外,您可能不需要方法中的模型参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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