dagilmore
dagilmore

Reputation: 35

Json as parameter to Grails controller

I am trying to pass a Json array to a Grails controller and then to a Java class. I can't figure out how to properly pass my params to the Java class though. Here is the relavent code.

AJAX POST:

  $('#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 Controller:

...
    def create() {

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

MatrixFactorization Constructor:

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

My console shows my Json array as:

[{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"}] 

My stack trace is:

| 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

I am very new to using JSON. Any help is appreciated. Thanks.

Upvotes: 2

Views: 3194

Answers (1)

Igor Artamonov
Igor Artamonov

Reputation: 35961

1.

jQuery will pass this data as request parameters, not JSON, by default. So you should build a JSON string to pass to jQuery. I can recommend you JSON 3 library. At this case it going to be:

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

2.

On server side you could also use standard Grails JSON converter (but you could use Gson, if you prefer), see http://grails.org/Converters+Reference.

At this case you can use

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

Upvotes: 3

Related Questions