Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > ColdFusion

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-18-2012, 09:41 AM   PM User | #1
anarchos78
New to the CF scene

 
Join Date: Oct 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
anarchos78 is an unknown quantity at this point
Cross-domain scripting ColdFusion issue

Greetings,
I have a silly question but I am a newbie. I am trying to make a cross-domain request (via ajax) using extjs4 library. As server side language I am using ColdFusion. The whole code that I have written so far is working on same domain. I need to separate static files (javascript, css, and html) placing them on Apache server and the dynamic content (cfm, CFC) placing them over Tomcat (openbd). So the front end scripts (on Apache->javascript mainly) making requests on Tomcat in order to fetch content. This definitely is cross-domain requests.
A code block that I am using in order to pull data (CFC) is:

Code:
<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>
The question is: how do I wrap the above function into a variable (named “callback”) and post it on client?

I have seen a similar code block on PHP but I don’t understand. Below is the PHP code block:

Code:
<?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);
} 
?>

With respect,

Tom

Greece
anarchos78 is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, coldfusion, cross-domain, extjs4.1

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:14 AM.


Advertisement
Log in to turn off these ads.