0

我有一个愚蠢的问题,但我是新手。我正在尝试使用 extjs4 库发出跨域请求(通过 ajax)。作为服务器端语言,我使用的是 ColdFusion。到目前为止,我编写的整个代码都在同一个域上工作。我需要将静态文件(javascript、css 和 html)放在 Apache 服务器上,将动态内容(cfm、CFC)放在 Tomcat(openbd)上。因此前端脚本(主要在 Apache->javascript 上)在 Tomcat 上发出请求以获取内容。这绝对是跨域请求。

我用来提取数据(CFC)的代码块是:

<cfcomponent output="false">
<cfprocessingdirective pageencoding="utf-8">
<cfset setEncoding("URL", "utf-8")>

    <cffunction name="getContent" access="remote" returnFormat="JSON" output="false" >  
        <cfargument name="start" default="0"/>    
        <cfargument name="limit" default="1000"/>
        <cfargument name="id" default="0" required="false" type="numeric">
        <cfargument name="model" default="" required="false" type="any">

        <cfset var arrNomoi = ArrayNew(1)>
        <cfset var stcReturn = "">

        <!--- When going back to base state, ExtJS will make the function call with start set to 0. If this is the case
        we set it to 1 --->
        <cfset Arguments.start = Arguments.start + 1>


        <cfif arguments.model EQ 'n_2664_1998'>

            <cfquery name="getNomoi" datasource="ktimatologio">
                SELECT CONCAT_WS('_',id,model) AS id, id AS 'id1', CONCAT_WS(' ',title,fek,date) AS 'title', description, body, model
                FROM n_2664_1998
                WHERE id = #arguments.id#
                ORDER BY id ASC
            </cfquery>

        <cfelseif arguments.model EQ 'n_2308_1995'>

            <cfquery name="getNomoi" datasource="ktimatologio">
                SELECT CONCAT_WS('_',id,model) AS id, id AS 'id1', CONCAT_WS(' ',title,fek,date) AS 'title', description, body, model
                FROM n_2308_1995
                WHERE id = #arguments.id#
                ORDER BY id ASC
            </cfquery>

        <cfelseif arguments.model EQ 'n_3889_2010'> 

            <cfquery name="getNomoi" datasource="ktimatologio">
                SELECT CONCAT_WS('_',id,model) AS id, id AS 'id1', CONCAT_WS(' ',title,fek,date) AS 'title', description, body, model
                FROM n_3889_2010
                WHERE id = #arguments.id#
                ORDER BY id ASC
            </cfquery>  

        </cfif>

        <cfset arrNomoi = convertQueryToExtJS(getNomoi,Arguments.start,Arguments.limit)>

        <cfset stcReturn = {"data"=arrNomoi,dataset=#getNomoi.RecordCount#}>

        <cfreturn stcReturn>

    </cffunction>

    <cffunction name="convertQueryToExtJS" access="public" hint="Convert Query to JSON usable by ExtJS Grid" returntype="array">
        <cfargument name="qryData" type="query" required="true" hint="Query to convert">
        <cfargument name="intStart" type="numeric" required="true" hint="Start of Result Set">
        <cfargument name="intLimit" type="numeric" required="true" hint="How many records to return">

        <!--- For the Array --->    
        <cfset var i = 1>        
        <cfset var end = ((Arguments.intStart) + arguments.intLimit)-1>
        <cfset var arrNomoi = ArrayNew(1)>

        <cfloop query="qryData" startrow="#Arguments.intStart#" endrow="#end#">        
            <cfset stcNomoi = StructNew()>
            <cfset stcNomoi['id'] = #qryData.id#>
            <cfset stcNomoi['id1'] = #qryData.id1#>
            <cfset stcNomoi['title'] = #qryData.title#>
            <!---<cfset stcNomoi['fek'] = #qryData.fek#>
            <cfset stcNomoi['date'] = #qryData.date#>--->
            <cfset stcNomoi['description'] = #qryData.description#>
            <cfset stcNomoi['body'] = #qryData.body#>
            <cfset stcNomoi['model'] = #qryData.model#>
            <cfset arrNomoi[i] = stcNomoi>
            <cfset i = i + 1>            
        </cfloop>


        <cfreturn arrNomoi>

    </cffunction>

</cfcomponent>

问题是:如何将上述函数包装成一个变量(名为“回调”)并将其发布到客户端?

我在 PHP 上看到过类似的代码块,但我不明白。下面是 PHP 代码块:

<?php
$callback = $_REQUEST['callback'];
// Create the output object.
$output = array('id' => 1, 'url' => 'loianegroner.com');
//start output
if ($callback) {
header('Content-Type: text/javascript');
echo $callback . '([' . json_encode($output) . ']);';
} else {
header('Content-Type: application/x-json');
echo json_encode($output);
} 
?>

尊重地,

汤姆

希腊

4

1 回答 1

0

看看这两个文件:

index.cfm 是您提交 AJAX 请求的处理程序,它将返回 JSON 结果

http://websvn.openbd.org/websvn/filedetails.php?repname=OpenBD&path=%2Ftrunk%2Fwebapp%2Fmanual%2Fapi%2Findex.cfm

index.cfm 与此 CFC 交互以获取信息并返回结果。

http://websvn.openbd.org/websvn/filedetails.php?repname=OpenBD&path=%2Ftrunk%2Fwebapp%2Fmanual%2Fapi%2Findex.cfc

当我编写这个 API 包装器时,我遇到了 returnFormat=JSON 的问题

于 2012-08-19T09:40:36.607 回答