2

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.

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

When I use stringify, my request.JSON is an empty object: [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
The above log is a printout of the array before the request and a printout of the return. In the grails controller, I am simply returning request.JSON
Try to add contentType: 'application/json' and dataType: 'json' options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.