使用jQuery AJAX JSON格式,你怎么输出一个.CFM页面来调用html页面的查询? [英] Using jquery ajax json format, How do you output a query from a .cfm page to the calling html page?

查看:159
本文介绍了使用jQuery AJAX JSON格式,你怎么输出一个.CFM页面来调用html页面的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地做​​到了返回一个字符串和cfdump教程。但我需要的语法与返回的项目清单的查询帮助。

I have successfully done tutorials that return a single string and a cfdump. But I need help on the syntax returning a query with a list of items.

另外半<一个href="http://stackoverflow.com/questions/9195811/how-do-you-output-a-query-from-a-cfm-page-using-jquery-ajax-with-json-format/9219325#9219325">repost因为它看起来像我的最后一个问题灭绝了的反应。

Also a semi repost since it looks like my last question died out on responses.

问题:我想输出使用jQuery从一个.CFM页面正在调用一个CFC JSON格式的查询。谁能告诉我我是什么洞错了吗?

Problem: I am trying to output a query using jquery with json format from a .cfm page that is calling a cfc. Can someone tell me what I am dong wrong?

我目前得到一个错误。

I currently am getting an error.

SyntaxError: JSON.parse: unexpected character.

在我的.html页面我有

In my .html page I have

   <html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript"> 
    $(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 = $("<tr />");
        var j = 0;

            $("#results").html(""); 

            for (var i = 0; i < resp["COLUMNS"].length; i++)
            {
                var tmp_th = $("<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 = $("<tr />");

                for (var i = 0; i < resp["DATA"][j].length; i++)
                {
                    var tmp_td = $("<td />");   
                    tmp_td.text(resp["DATA"][j][i]);
                    tmp_html.append(tmp_td);
                }
                $("#results").append(tmp_html);
            }

    }

    })
    </script>

</head>

<body>
    <input type="button" id="runQuery" value="Show Teams" />
    <input type="text" name="name">

    <table id="results" cellspacing="0" cellpadding="0">
</table>


</body>
</html>

在我test.cfm调用页面我有

In my test.cfm calling page I have

   <cfinvoke 
   component="learncf_jquery" 
   method="getAllTeams" 
   returnVariable="getItems">
   </cfinvoke>

  <cfoutput>#SerializeJSON(getItems)#</cfoutput>

test.cfm输出看起来像

test.cfm output looks like

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

learncf_jquery.cfc(从另一个教程code)

learncf_jquery.cfc (code from another tutorial)

<cfcomponent name="jQueryExample" output="false">

<cffunction name="getAllPlayers" output="false" access="private" returntype="query">
    <cfset var qAllPlayers = queryNew('playerName, team') />

    <cfset queryAddRow(qAllPlayers, 40) />

    <!--- add 10 Giants players to the "database" --->
    <cfset querySetCell(qAllPlayers, 'playerName', 'Alford, Jay', 1) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 1) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Barden, Ramses', 2) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 2) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Beckum, Travis', 3) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 3) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Bernard, Rocky', 4) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 4) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Blackburn, Chase', 5) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 5) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Boss, Kevin', 6) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 6) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Bradshaw, Ahmad', 7) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 7) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Canty, Chris', 8) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 8) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Diehl, David', 9) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 9) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Feagles, Jeff', 10) />
    <cfset querySetCell(qAllPlayers, 'team', 'NY Giants', 10) />

    <!--- add 10 Cowboys players to the "database" --->
    <cfset querySetCell(qAllPlayers, 'playerName', 'Adams, Flozell', 11) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 11) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Austin, Miles', 12) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 12) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Brown, Courtney', 13) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 13) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Choice, Tashard', 14) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 14) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Colombo, Marc', 15) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 15) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Davis, Leonard', 16) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 16) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Jones, Felix', 17) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 17) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Kitna, Jon', 18) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 18) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Procter, Corey', 19) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 19) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Romo, Tony', 20) />
    <cfset querySetCell(qAllPlayers, 'team', 'Dallas Cowboys', 20) />

    <!--- add 10 Eagles players to the "database" --->
    <cfset querySetCell(qAllPlayers, 'playerName', 'Akers, David', 21) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 21) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Baskett, Hank', 22) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 22) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Booker, Lorenzo', 23) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 23) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Clemons, Chris', 24) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 24) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Demps, Quintin', 25) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 25) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Feeley, A.J.', 26) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 26) />

    <cfset querySetCell(qAllPlayers, 'playerName', 'Hobbs, Ellis', 27) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 27) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Jackson, DeSean', 28) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 28) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Klecko, Dan', 29) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 29) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'McNabb, Donovan', 30) />
    <cfset querySetCell(qAllPlayers, 'team', 'Philadelphia Eagles', 30) />

    <!--- add 10 Redskins players to the "database" --->
    <cfset querySetCell(qAllPlayers, 'playerName', 'Alexander, Lorenzo', 31) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 31) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Campbell, Jason', 32) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 32) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Clark, Devin', 33) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 33) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Cooley, Chris', 34) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 34) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Dixon, Antonio', 35) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 35) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Fletcher, London', 36) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 36) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Hackett, D.J.', 37) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 37) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Randle El, Antwaan', 38) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 38) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Smoot, Fred', 39) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 39) />
    <cfset querySetCell(qAllPlayers, 'playerName', 'Suisham, Shaun', 40) />
    <cfset querySetCell(qAllPlayers, 'team', 'Washington Redskins', 40) />

    <cfreturn qAllPlayers />
</cffunction>

<cffunction name="getAllTeams" access="remote" output="false" returntype="query">
    <cfset var allPlayers   = getAllPlayers() />
    <cfset var qGetAllTeams = "" />

    <cfquery name="qGetAllTeams" dbtype="query">
        SELECT DISTINCT team FROM allPlayers ORDER BY team
    </cfquery>

    <cfreturn qGetAllTeams />
</cffunction>

推荐答案

我已经测试你的code在我的本地主机,它做工精细,用code没有问题。在你的情况下,唯一的问题可能是你已经启用了您的ColdFusion调试输出,可能会妨碍你的JSON格式。添加该code:&LT; cfsetting ShowDebugOutput =FALSE/&GT; 在生成JSON响应页面的顶部(在你的情况下test.cfm) ,禁用ColdFusion的调试器。测试,让我知道你发现了什么。欲了解更多信息请访问以下网址。 <一href="http://www.mindfiresolutions.com/While-working-with-AJAX-ColdFusion-Debugging-may-break-your-Ajax-calls-1477.php" rel="nofollow">http://www.mindfiresolutions.com/While-working-with-AJAX-ColdFusion-Debugging-may-break-your-Ajax-calls-1477.php

I have tested your code in my local box and it is working fine, no problem with the code. The only problem in your case may be that you have enabled your coldfusion debugging output which may hamper your json format. Add this code: <cfsetting ShowDebugOutput = "false"/> in the top of the page that generates the json response(in you case test.cfm), to disable the coldfusion debugger. Test and let me know what you find. For more info visit the following URL. http://www.mindfiresolutions.com/While-working-with-AJAX-ColdFusion-Debugging-may-break-your-Ajax-calls-1477.php

否则,你的回应页必须有它妨碍了JSON响应其他一些不必要的字符串

Otherwise, in your response page there must be some other unwanted string which is hampering the json response

这篇关于使用jQuery AJAX JSON格式,你怎么输出一个.CFM页面来调用html页面的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆