Question

Apologies if I'm haven't found it in the documentation yet...

Q: How do you change the queryformat for an ajax call in Railo? Here's my component:

component {

    remote function Read() returnformat='json' {
        svc = new Query();
        svc.setSQL("SELECT * FROM INFORMATION_SCHEMA.TABLES");
        obj = svc.execute();
        local.result.Prefix = obj.getPrefix();
        local.result.qry = obj.getResult();
        url.queryFormat = "column";
        return local.result;
    }
    }

and here's my JavaScript:

(function() {
    var local = {};

    local.type = 'POST';
    local.url = 'AJAX.cfc';
    local.dataType = 'json';
    local.data = {};
    local.data.method = 'Read';
    local.Promise = $.ajax(local);
    local.Promise.done(done);
    local.Promise.fail(fail);

    function done(response) {
        console.log(response);
        debugger;
    }
    function fail(xhr,status,response) {
        debugger;
    }
})();

What I'm getting back is:

response.qry.DATA[] // 57 arrays, each of length 4

But ColdFusion returns this, which I've grown fond of using (being able to use the column names instead of the array position):

response.qry.DATA.TABLE_CATALOG[] // An array of 57 elements
response.qry.DATA.TABLE_SCHEMA[]
response.qry.DATA.TABLE_NAME[]
response.qry.DATA.TABLE_TYPE[]
Was it helpful?

Solution

Use ReturnFormat="plain" on the function, and pass true for the 2nd argument of serializeJson()

serializeJson(Query, true)

That will give you a JSON object that is serialized by Column, so you can just return it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top