如何使用JSON格式的jQuery AJAX从.cfm页面输出查询?

我试图使用json格式的jquery从.cfm页面输出查询。 谁能告诉我,我错了什么?

编辑: 在这里回答

编辑:刚刚发布了我的cfc代码

编辑:我目前收到错误。

SyntaxError: JSON.parse: unexpected character. 

test.cfm输出看起来像

 {"COLUMNS":["TEAM"],"DATA":[["Dallas Cowboys"],["NY Giants"],["Philadelphia Eagles"],["Washington Redskins"]]} 

在我的.html页面中,我有

      $(document).ready(function(){ $("#runQuery").click(function () { $.ajax({ type: "GET", url: "./test.cfm", dataType: "json", success: function (resp, textStatus, jqXHR) { buildResultsTable(resp); }, error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); } }); }); function buildResultsTable(resp) { var tmp_html = $(""); var j = 0; $("#results").html(""); for (var i = 0; i < resp["COLUMNS"].length; i++) { var tmp_th = $(""); tmp_th.text(resp["COLUMNS"][i]); tmp_html.append(tmp_th); } $("#results").append(tmp_html); for (j = 0; j < resp["DATA"].length; j++) { tmp_html = $(""); for (var i = 0; i < resp["DATA"][j].length; i++) { var tmp_td = $(""); tmp_td.text(resp["DATA"][j][i]); tmp_html.append(tmp_td); } $("#results").append(tmp_html); } } })      

在我的test.cfm调用页面中,我有

    #SerializeJSON(getItems)# 

learncf_jquery.cfc

                                                                                               SELECT DISTINCT team FROM allPlayers ORDER BY team    

./test.cfm:

    #SerializeJSON(getItems)# 

这是我修改的一些代码,以匹配您的示例:

我的HTML:

 

我的javascript,它将查询结果放入表中:

  $("#runQuery").click(function () { $.ajax({ type: "GET", url: "./test.cfm", dataType: "json", success: function (resp, textStatus, jqXHR) { buildResultsTable(resp); }, error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); } }); }); function buildResultsTable(resp) { var tmp_html = $(""); var j = 0; $("#results").html(""); for (var i = 0; i < resp["COLUMNS"].length; i++) { var tmp_th = $(""); tmp_th.text(resp["COLUMNS"][i]); tmp_html.append(tmp_th); } $("#results").append(tmp_html); for (j = 0; j < resp["DATA"].length; j++) { tmp_html = $(""); for (var i = 0; i < resp["DATA"][j].length; i++) { var tmp_td = $(""); tmp_td.text(resp["DATA"][j][i]); tmp_html.append(tmp_td); } $("#results").append(tmp_html); } } 

您的test.cfm页面需要输出getItems作为JSON。 尝试将此添加到test.cfm:

  #SerializeJSON(getItems)#  

您也可以让CF以json格式从CFC返回查询:(这可能与将序列化函数放在上面相同)

    SELECT * FROM subcategory WHERE topCatID = #Arguments.catID# ORDER BY subcat