【问题标题】:Json as parameter to Grails controllerJson 作为 Grails 控制器的参数
【发布时间】:2013-01-01 21:32:45
【问题描述】:

我正在尝试将 Json 数组传递给 Grails 控制器,然后再传递给 Java 类。我不知道如何正确地将我的参数传递给 Java 类。这是相关代码。

AJAX 发布:

  $('#matrixForm').submit(function(e) {
        e.preventDefault();

    var matrixArray = $(this).serializeArray();

    $.ajax({  
        type: "POST",  
        data: matrixArray,  
        url: "/turingpages/factorize/create",
        success: function(data) {
            //USE DATA
        }  
    });  
    }); 

Grails 控制器:

...
    def create() {

        MatrixFactorization m = new MatrixFactorization(params)
        Gson gson = new Gson()
        def jsonMatrix = gson.toJson(m.answer)
        render jsonMatrix
    }
...

矩阵分解构造函数:

public MatrixFactorization(JsonElement jsonarray) {
    BlockRealMatrix R = GsonMatrix.toMatrix(jsonarray);
    this.run(R);
}

我的控制台将我的 Json 数组显示为:

[{name:"00", value:"1"}, {name:"01", value:"2"}, {name:"02", value:"3"}, {name:"10", value:"4"}, {name:"11", value:"0"}, {name:"12", value:"4"}, {name:"20", value:"0"}, {name:"21", value:"4"}, {name:"22", value:"2"}] 

我的堆栈跟踪是:

| Error 2013-01-18 00:30:23,792 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver  - GroovyRuntimeException occurred when processing request: [POST] /turingpages/factorize/create - parameters:
21: 4
20: 0
10: 4
22: 2
00: 1
01: 2
11: 0
02: 3
12: 4
failed to invoke constructor: public matrices.MatrixFactorization(com.google.gson.JsonElement) with arguments: [] reason: java.lang.IllegalArgumentException: wrong number of arguments. Stacktrace follows:
Message: failed to invoke constructor: public matrices.MatrixFactorization(com.google.gson.JsonElement) with arguments: [] reason: java.lang.IllegalArgumentException: wrong number of arguments
    Line | Method
->>   15 | create    in turingpages.rest.MFController$$ENuqtska
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    195 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run       in java.lang.Thread

我对使用 JSON 很陌生。任何帮助表示赞赏。谢谢。

【问题讨论】:

    标签: java jquery ajax json grails


    【解决方案1】:

    1.

    默认情况下,jQuery 会将这些数据作为请求参数而不是 JSON 传递。所以你应该构建一个 JSON 字符串来传递给 jQuery。我可以推荐你JSON 3 library。在这种情况下,它将是:

    $.ajax({  
        type: "POST",  
        data: JSON.stringify(matrixArray),  
        url: "/turingpages/factorize/create",
        success: function(data) {
            //USE DATA
        }  
    });  
    

    2.

    在服务器端,您还可以使用标准 Grails JSON 转换器(但如果您愿意,也可以使用 Gson),请参阅 http://grails.org/Converters+Reference

    在这种情况下你可以使用

    def create() {
        MatrixFactorization m = new MatrixFactorization(request.JSON)
        render m.answer as JSON
    }
    

    【讨论】:

    • 当我使用 stringify 时,我的 request.JSON 是一个空对象:[15:52:12.302] {"array":[[1,2,3],[3,4,5],[5,6,7]]} @ http://localhost:8080/turingpages/js/dataFeed.js:15 [15:52:12.499] POST http://localhost:8080/turingpages/factorize/create [HTTP/1.1 200 OK 18ms] [15:52:12.528] {} @ http://localhost:8080/turingpages/js/dataFeed.js:22
    • 以上日志是请求前的数组打印输出和返回的打印输出。在 grails 控制器中,我只是返回 request.JSON
    • 尝试添加contentType: 'application/json'dataType: 'json'选项
    猜你喜欢
    • 2019-04-03
    • 1970-01-01
    • 2015-09-21
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多