跨域脚本ColdFusion问题

时间:2012-08-18 08:37:07

标签: ajax coldfusion cross-domain extjs4.1

我有一个愚蠢的问题,但我是一个新手。我正在尝试使用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>

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

我在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);
} 
?>

尊重,

汤姆

希腊

1 个答案:

答案 0 :(得分: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

的问题