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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28889074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:21:04  来源:igfitidea点击:

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

javajqueryajaxspring-mvc

提问by user1945064

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

这也不会生成成功警报。请建议

回答by Bouhanef

Your method should return directly the list, as a json, no need to put it in the model and return the model. Also check if you have an error in your callback ajax.

你的方法应该直接返回列表,作为一个json,不需要把它放在模型中并返回模型。还要检查您的回调ajax 中是否有错误。

回答by wahwahwah

You are specifying dataType: 'JSON'in your ajax call, but you are not converting your response object (model) to json in your controller.

dataType: 'JSON'在 ajax 调用中指定,但没有在控制器中将响应对象(模型)转换为 json。

From the jQuery ajax documentationfor dataTypesetting:

来自 jQuery ajax 文档dataType设置:

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 modelto json in your response. For example (using json-io):

您可能想要引用一个 json 序列化程序(除非您想自己编写)-但重要的部分是model在您的响应中将您的序列化为json。例如(使用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 对象的小提琴示例

回答by Mark.zxx

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>代替。