$ .ajax ColdFusion cfc JSON Hello World [英] $.ajax ColdFusion cfc JSON Hello World

查看:184
本文介绍了$ .ajax ColdFusion cfc JSON Hello World的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尽量简化了这个例子。我有一个远程函数:

I've simplified this example as much as I can. I have a remote function:

<cfcomponent output="false">
<cffunction name="Read" access="remote" output="false">
    <cfset var local = {}>

    <cfquery name="local.qry" datasource="myDatasource">
    SELECT PersonID,FirstName,LastName FROM Person
    </cfquery>
    <cfreturn local.qry>
</cffunction>
</cfcomponent>

使用jQuery $ .ajax方法,我想创建一个无序的每个人的列表。 / p>

And using the jQuery $.ajax method, I would like to make an unordered list of everyone.

    <!DOCTYPE HTML>
    <html>
    <head>
    <script src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load("jquery", "1");
    </script>
    <script type="text/javascript">
    jQuery(function($){
    $.ajax({
            url: "Remote/Person.cfc?method=Read&ReturnFormat=json",
            success: function(data){
                var str = '<ul>';
// This is where I need help:
                for (var I=0; I<data.length; I++) {
                    str += '<li>' + I + data[I][1]+ '</li>'
                }
                str += '</ul>';
                $('body').html(str);
            },
            error: function(ErrorMsg){
               console.log("Error");
            }
        });
    });
    </script>
    </head>
    <body>
    </body>
    </html>

我失去的部分是循环数据的地方。
我更喜欢使用jQuery $ .ajax方法,因为我理解$ .get和$ .post没有错误捕获。

The part where I'm lost is where I'm looping over the data. I prefer to the use jQuery $.ajax method because I understand that $.get and $.post don't have error trapping.

t知道如何处理从cfc返回的JSON。

I don't know how to handle JSON returned from the cfc.

推荐答案

看起来像是json格式的结果href =http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_21.html =nofollow noreferrer> http://livedocs.adobe.com/coldfusion/8/ htmldocs / help.html?content = Tags_f_21.html )。
如果指定returnformat =json并且函数返回查询,ColdFusion将查询序列化为具有两个条目的JSON对象,以及列名称数组和列数据数组数组。有关详细信息,请参阅SerializeJSON。 http://livedocs.adobe.com/coldfusion/ 8 / htmldocs / help.html?content = Tags_f_21.html

Looks like the result is in json format(check the docs http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_21.html). "If you specify returnformat="json" and the function returns a query, ColdFusion serializes the query into a JSON Object with two entries, and array of column names, and an array of column data arrays. For more information see SerializeJSON." http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_21.html

所以第一个数组(data.COLUMNS应该是一个列名数组。 COLUMNS [0]会给你第一列的名称。data.DATA [0]会给你查询的第一行。

So the first array(data.COLUMNS should be an array of column names. data.COLUMNS[0] would give you the name of the first column. data.DATA[0] would give you the first row of the query.

一个好办法是请使用chrome或firebug控制台中的console.log(data)来查看其结构化形式的数据。

A nice trick is to use console.log(data) in chrome or firebug console to view the data in it's structured form.

我没有测试这个,但它应该是close。从数据生成基本表。

I didn't test this, but it should be close. Just generating a basic table from the data.

$.ajax({
    url: 'Remote/Person.cfc?method=Read&ReturnFormat=json',
    dataType: 'json',
    success: function(response) {
        var str = '<table><tr>';
        var i;
        var j;

        //loop over each column name for headers
        for (i = 0; i < response.COLUMNS.length; i++) {
              str += '<th>' + response.COLUMNS[i] + '</th>';
          }
        }
        str += '</tr>';

        //loop over each row
        for (i = 0; i < response.DATA.length; i++) {
          str += '<tr>';
          //loop over each column
          for (j = 0; j < response.DATA[i].length; j++) {
              str += '<td>' + response.DATA[i][j] + '</td>';
          }
          str += '</tr>';
        }
        str += '</table>';

        $('body').html(str);
    },
    error: function(ErrorMsg) {
       console.log('Error');
    }
});

这篇关于$ .ajax ColdFusion cfc JSON Hello World的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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