fsi
fsi

Reputation: 1367

Ajax is success, but Grails in JSON is null

In sencha, AJAX is always successfuly, but in grails fails whenever i try to recover him. It should recover in def data = params.data, and def data = paramsdidnt work.

controller in sencha(on save)

onSave: function(button, e, eOpts) {
    var name = Ext.getCmp('name').getValue();
    var description = Ext.getCmp('description').getValue();
    var data = {};
    data.name = name;
    data.description = description;

    Ext.Ajax.request({
    type: 'POST',
    contentType:'application/json',
    dataType: 'jsonp',
    data: data,
    crossDomain: true,
    url: 'http://localhost:8080/TryIt/product/newProduct',
    success: function(response, opts) {
    console.log('server-side success with status code ' + response.status);
    },
    failure: function(response, opts) {
    console.log('server-side failure with status code ' + response.status);
    },
    error: function(msg){
    alert(msg);
    }
});

controller in grails

    def newProduct() {
    def data = JSON.parse(data)
    def newP = new Product(name: data.name, description: data.description)
    if (newP) {
            newP = newP.save(flush: true)
            println "saved!"
            println data.name
            println data.description
        } else {
        println "error"
        }
        return result as JSON
    }

Question is, how to recover the data that is handed to grails? O yeah, js and jquery do this like rainbow and butterflies(works good).

Upvotes: 0

Views: 1081

Answers (3)

fsi
fsi

Reputation: 1367

I fixed using data = JSON.stringify(data); and changing the data to params in my Ajax.

Upvotes: 1

codelark
codelark

Reputation: 12334

You can extract the json data with:

def jsonObject = request.JSON

edit: I should add that it is already parsed into a JSONObject so you will not need to call JSON.parse()

Upvotes: 0

rixo
rixo

Reputation: 25031

If your request really is crossing domains, you won't be able to use Ext.Ajax, you'll have to use Ext.data.JsonP instead. But apparently it is not, or you wouldn't see it on the server side at all.

In any event, neither Ext.Ajax#request nor Ext.data.JsonP#request accept a data option. So your request may be leaving, but it's leaving empty. Check it in the network panel of your browser's developer tool.

Then, if you stick with Ext.Ajax, use the jsonData param instead of data, or with JsonP, use params.

Upvotes: 1

Related Questions