【问题标题】:How to call "Controller".listJson() from dojo.xhrGet如何从 dojo.xhrGet 调用“Controller”.listJson()
【发布时间】:2015-03-05 12:53:54
【问题描述】:

我是使用 spring roo、ajax 和 json 的新手,我有一个问题。如果您使用一些 roo shell 命令,Roo 在控制器类中创建两种方法来列出实体的所有元素,一种用于 ajax/json,一种不用于 json。

ajax/json 方法注解是

@RequestMapping(headers = "Accept=application/json")
 @ResponseBody
 public ResponseEntity<String> "Controller".listJson() 

另一种方法是:

 @RequestMapping(produces = "text/html")
     public String "Controller".list

这两种方法都有这个路径@RequestMapping("/admtpcomunidads")

当我尝试使用 dojo 从 javascript 调用 ajax/json 方法时,我使用:

dojo.xhrGet({url: "admtpcomunidads",
              handleAs: 'json',

但是调用链接到“Controller.list”方法而不是“Controller.listJson”方法。我需要在 dojo.xhrGet 调用中使用什么 url 来链接 listJson 方法?

谢谢

【问题讨论】:

    标签: spring-roo


    【解决方案1】:

    我不知道 Dojo,但从 jquery 我做到了:

    $.ajax({
            type: "GET",
            url:"/[projectName]/[EntityName]s",    
            dataType: "json",
            contentType: "application/json",
            beforeSend: function(xhrObj)
            {
              xhrObj.setRequestHeader("Accept","application/json");
            },
    
        }).done(function(data ) {             
               $.each(data, function(index) {
               alert(data[index].field)                
                });
    
        }).fail(function(jqXHR, textStatus) {       
                alert( "error: " +jqXHR.responseText +"-status: "+textStatus + "status:" +jqXHR.status);
                });
    

    【讨论】:

    • 谢谢阿里,但问题是spring roo创建了两个我认为可以匹配相同url的方法,在这种情况下,如果我使用url“projectName/EntitiName”匹配的方法是:@RequestMapping(produces = "text/html") public String "Controller".list 但是我想匹配这个方法:@RequestMapping(headers = "Accept=application/json") @ResponseBody public ResponseEntityController" .listJson() 也许我需要在请求头中放一些数据来匹配 listJson 方法,但我不知道怎么做。
    • 我认为你可以: headers:{ "Content-Type": "application/json" , "Accept": "application/json" }, handleAs: "json",
    • 感谢阿里,我已经测试了标题:{ "Content-Type": "application/json" , "Accept": "application/json" }, handleAs: "json" 但我得到了一个“ NetworkError: 406 Firebug 控制台中没有可接受的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 2013-09-29
    • 2021-05-29
    • 2011-11-03
    • 2018-02-19
    • 2012-07-23
    相关资源
    最近更新 更多