Steve:

Your da man! Thanks! I had started down that path when you replied - but I was 
trying to prepend the serialized JSON string with the "//". When I saw your 
snippet I realized that was unnecessary. It is working now!

Sincerely, Brooks
________________________________
Brooks Wilson  |  Senior Web Developer Programmer/Analyst
Technology Solutions Services  |  Application Delivery Services
Federal Reserve Bank of Atlanta  |  1000 Peachtree Street,  Atlanta, GA 
30309-4470
Phone: 404.498.8178 | Fax: 404.498.8239 | Mobile: 404.985.9270
Email: brooks.wil...@atl.frb.org<mailto:brooks.wil...@atl.frb.org>

From: ad...@acfug.org [mailto:ad...@acfug.org] On Behalf Of Steve Drucker
Sent: Thursday, December 05, 2013 2:04 PM
To: discussion@acfug.org
Subject: Re: [ACFUG Discuss] Returning JSONP with Coldfusion 9 cfc

Try this:


<cffunction name="getDataJSONP"
access="remote"
returntype="string"
output="false"
returnformat="plain">

<cfargument name="callback" type="string" required="yes">

<cfset local.q = "">
<cfquery name="local.q">

select firstname, lastname

from people

</cfquery>



<cfreturn arguments.callback & "(" & serializejson(q,true) & ")>

</cffunction>

On Thu, Dec 5, 2013 at 1:59 PM, Steve Drucker 
<sdruc...@figleaf.com<mailto:sdruc...@figleaf.com>> wrote:
It's covered here:

http://training.figleaf.com/curriculum/cfjqm.cfm

On Thu, Dec 5, 2013 at 1:57 PM, Wilson, Brooks 
<brooks.wil...@atl.frb.org<mailto:brooks.wil...@atl.frb.org>> wrote:
Greetings:

I'm beginning to think it isn't possible for CF 9.01 to return JSONP? I am 
using jQuery 1.10.2 on a client to request a JSONP object. See the following 
HTML (client-side):

var jqxhr = $.getJSON( "WeatherFeedJsonp.cfc?callback=?"
       , {method: "get5DayForecastJsonp", latitude: "33.76", longitude: 
"-84.42"}
       , function(data) { loadData(data); } // callback function
       )
       .fail(function(jqxhr, textStatus, error) {
             var err = textStatus + ", " + err; alert(err);
       });

The WeatherFeedJsonp.cfc method get5DayForecastJsonP is coded (server-side):

<cffunction name="get5DayForecastJsonp" access="remote" returntype="array" 
returnFormat="JSON">
       <cfargument name="latitude" type="Numeric" required="true"/>
       <cfargument name="longitude" type="Numeric" required="true"/>

       <cfset var data = get5DayForecastXml(latitude, longitude)/> <!--- 
returns NOAA XML weather feed --->
       <cfset var data = xmlParse(data)/>
       <cfset var xmlRoot = data['dwml']['data']/>
       <cfset var weather = arrayNew(1)/>

<!---
       .
       .
       .
  loop through the XML data variable that contains the 5 day forecast from NOAA
  and create an array of daily weather forecast in the "weather" variable"
       .
       .
       .
--->

       <cfreturn weather/>
</cffunction>

I added

<cfset this.secureJSON = true>
<cfset this.secureJSONPrefix = "// ">

to application.cfc .

On the client, I tested by visiting the URL: 
http://myservername.org/componentName.cfc?method=get5DayForecastJsonp&latitude=...
 And I get a nice looking JSON string back:

// 
[{"ICON":"hi_shwrs50.jpg","LOTEMP":"62","PCTRAINNITE":"48","CONDITIONS":"Chance 
Rain 
Showers","DATE":"2013-12-05","PCTRAINDAY":"54","HITEMP":"72"},{"ICON":"hi_shwrs80.jpg","LOTEMP":"49","PCTRAINNITE":"80","CONDITIONS":"Rain
 Showers 
Likely","DATE":"2013-12-06","PCTRAINDAY":"72","HITEMP":"75"},{"ICON":"ra.jpg","LOTEMP":"47","PCTRAINNITE":"80","CONDITIONS":"Drizzle","DATE":"2013-12-07","PCTRAINDAY":"70","HITEMP":"54"},{"ICON":"ra80.jpg","LOTEMP":"53","PCTRAINNITE":"67","CONDITIONS":"Rain","DATE":"2013-12-08","PCTRAINDAY":"80","HITEMP":"56"},{"ICON":"hi_shwrs70.jpg","LOTEMP":"40","PCTRAINNITE":"49","CONDITIONS":"Rain
 Showers Likely","DATE":"2013-12-09","PCTRAINDAY":"70","HITEMP":"61"}]

But when I execute the code with the $.getJSON call via HTML I get an error. In 
FireBug I get a 200 OK and the data appears as XHR not JavaScript. I know that 
the content type returned in the response must be text/javascript, so I used

<cfcontent type="text/javascript; charset=UTF-8" />

In the CFC component body. The content-type response header says 
"text/javascript", but it still shows up as XHR.

Has anyone implemented a JSONP feed with CF 9? What am I missing?

Sincerely, Brooks
________________________________
Brooks Wilson  |  Senior Web Developer Programmer/Analyst
Technology Solutions Services  |  Application Delivery Services
Federal Reserve Bank of Atlanta  |  1000 Peachtree Street,  Atlanta, GA 
30309-4470
Phone: 404.498.8178<tel:404.498.8178> | Fax: 404.498.8239<tel:404.498.8239> | 
Mobile: 404.985.9270<tel:404.985.9270>
Email: brooks.wil...@atl.frb.org<mailto:brooks.wil...@atl.frb.org>


-------------------------------------------------------------
To unsubscribe from this list, manage your profile @
http://www.acfug.org?fa=login.edituserform

For more info, see http://www.acfug.org/mailinglists
Archive @ http://www.mail-archive.com/discussion%40acfug.org/
List hosted by FusionLink<http://www.fusionlink.com>
-------------------------------------------------------------



--
Regards,
Steve Drucker
Founder / President
Fig Leaf Software
http://www.figleaf.com
http://training.figleaf.com
Acquia, Adobe, Brightcove, Google, JackBe, Paperthin, Sencha 
Consulting/Training/Sales/Support

"We've got you covered"

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on GSA Schedule



--
Regards,
Steve Drucker
Founder / President
Fig Leaf Software
http://www.figleaf.com
http://training.figleaf.com
Acquia, Adobe, Brightcove, Google, JackBe, Paperthin, Sencha 
Consulting/Training/Sales/Support

"We've got you covered"

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on GSA Schedule



-------------------------------------------------------------

To unsubscribe from this list, manage your profile @ 

http://www.acfug.org?fa=login.edituserform



For more info, see http://www.acfug.org/mailinglists

Archive @ http://www.mail-archive.com/discussion%40acfug.org/

List hosted by http://www.fusionlink.com

-------------------------------------------------------------


Reply via email to