Ajax没有返回JSON数据

时间:2017-01-18 10:07:55

标签: jquery sql json ajax groovy

我尝试执行一个简单的Ajax请求,该请求将调用groovy脚本并返回脚本生成的JSON字符串

这是我的 Ajax:

var myUrl = "connections.groovy";

$.ajax({
    url : myUrl,
    type: 'GET',
    success : function(results) {
        console.log(results)
    }
});

以下groovy脚本用于从数据库中收集JSON并将其返回到html页面

connections.groovy

def cellTabel() {

    def db = [url:'jdbc:mysql://###.##.#.##', user:'###', password:'###', driver:'com.mysql.jdbc.Driver']
    def resultset = Sql.newInstance(db.url, db.user, db.password, db.driver).rows('SELECT count(*) as NumberOfRecords,work_station,sum(marshalled) as marshalled,sum(picked) as picked, sum(complete) as complete FROM vantec.document_header where date_created >= CURRENT_DATE() and work_station != "" group by work_station')
    return new JsonBuilder(resultset).toString()
}

cellTabel();    

如果我在一个groovy控制台中运行groovy代码,它会工作并生成我正在寻找的JSON代码但是当我从页面上的ajax运行它时我什么都没有回来< / p>

这是我期待的JSON

[{"NumberOfRecords":10,"work_station":"CELL1","marshalled":10,"picked":10,"complete":10},{"NumberOfRecords":19,"work_station":"CELL2","marshalled":19,"picked":19,"complete":19},{"NumberOfRecords":10,"work_station":"CELL3","marshalled":10,"picked":10,"complete":10},{"NumberOfRecords":10,"work_station":"CELL4","marshalled":10,"picked":10,"complete":10},{"NumberOfRecords":6,"work_station":"CELL5","marshalled":6,"picked":6,"complete":6},{"NumberOfRecords":1,"work_station":"CELL6","marshalled":0,"picked":1,"complete":1},{"NumberOfRecords":9,"work_station":"CELL7","marshalled":1,"picked":7,"complete":7},{"NumberOfRecords":1,"work_station":"CELL8","marshalled":0,"picked":1,"complete":1},{"NumberOfRecords":10,"work_station":"CELL9A","marshalled":6,"picked":9,"complete":9},{"NumberOfRecords":9,"work_station":"CELL9B","marshalled":6,"picked":8,"complete":8}]

这是我在groovy控制台中运行connections.groovy时得到的JSON

修改 控制台中的响应 console response 网络选项卡打开 network tab

1 个答案:

答案 0 :(得分:2)

将你的groovlet更改为:

// other stuff
def cellTabel() {

    def db = [url:'jdbc:mysql://###.##.#.##', user:'###', password:'###', driver:'com.mysql.jdbc.Driver']
    def resultset = Sql.newInstance(db.url, db.user, db.password, db.driver).rows('SELECT count(*) as NumberOfRecords,work_station,sum(marshalled) as marshalled,sum(picked) as picked, sum(complete) as complete FROM vantec.document_header where date_created >= CURRENT_DATE() and work_station != "" group by work_station')
    return new JsonBuilder(resultset).toString()
}

response.contentType = 'application/json'
println cellTabel()