Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 01:05
Show Gist options
  • Save bennadel/9753397 to your computer and use it in GitHub Desktop.
Save bennadel/9753397 to your computer and use it in GitHub Desktop.
Ask Ben: Cross-Site AJAX Requests Using jQuery And ColdFusion
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Cross-Site jQuery AJAX Demo With Proxy</title>
<script type="text/javascript" src="jquery-1.3.1.pack.js"></script>
<script type="text/javascript">
// Run when DOM is ready.
$(
function(){
var jForm = $( "form" );
// Hook up form submit.
jForm.submit(
function( objEvent ){
// Get weather from Yahoo.
GetWeather( jForm );
// Prevent default.
return( false );
}
);
}
);
// This method actually performs the cross-site AJAX
// using a proxy ColdFusion page.
function GetWeather( jForm ){
// Hook up form submit to pull down weather from
// Yahoo API using proxy AJAX request.
$.ajax(
{
url: "ajax_proxy.cfm",
type: "get",
dataType: "xml",
cache: false,
// As part of the data, we have to pass in the
// the target url for our server-side AJAX request.
data: {
proxyURL: "http://weather.yahooapis.com/forecastrss",
p: jForm.find( ":text" ).val()
},
// Alert when content has been loaded.
success: function( xmlData ){
// Get the content from the response XML.
var strData = $( xmlData )
.find( "description" )
.text()
;
// Load content into DOM.
$( "#content" ).html( strData );
}
}
);
}
</script>
</head>
<body>
<h1>
Cross-Site jQuery AJAX Demo With Proxy
</h1>
<form>
<p>
Zip Code:<br />
<input type="text" name="zip" />
<input type="submit" value="Get Weather" />
</p>
</form>
<div id="content">
<!-- Here, we will store the AJAX response. -->
</div>
</body>
</html>
<!---
Check to see if the page request is a POST or a GET.
Based on this, we can figure out our target URL.
--->
<cfif (CGI.request_method EQ "get")>
<!--- Get URL-based target url. --->
<cfset strTargetURL = URL.ProxyURL />
<!--- Delete target URL. --->
<cfset StructDelete( URL, "ProxyURL" ) />
<cfelse>
<!--- Get FORM-based target url. --->
<cfset strTargetURL = FORM.ProxyURL />
<!--- Delete target URL. --->
<cfset StructDelete( FORM, "ProxyURL" ) />
</cfif>
<!---
Remove any AJAX anit-caching that was used by jQuery. This
is a random number meant to help ensure that GET URLs are
not cached.
--->
<cfset StructDelete( URL, "_" ) />
<!---
Make the proxy HTTP request using. When we do this, try to
pass along all of the CGI information that was made by the
original AJAX request.
--->
<cfhttp
result="objRequest"
url="#UrlDecode( strTargetURL )#"
method="#CGI.request_method#"
useragent="#CGI.http_user_agent#"
timeout="15">
<!--- Add the referer tht was passed-in. --->
<cfhttpparam
type="header"
name="referer"
value="#CGI.http_referer#"
/>
<!--- Pass along any URL values. --->
<cfloop
item="strKey"
collection="#URL#">
<cfhttpparam
type="url"
name="#LCase( strKey )#"
value="#URL[ strKey ]#"
/>
</cfloop>
<!--- Pass along any FORM values. --->
<cfloop
item="strKey"
collection="#FORM#">
<cfhttpparam
type="formfield"
name="#LCase( strKey )#"
value="#FORM[ strKey ]#"
/>
</cfloop>
</cfhttp>
<!---
<!--- Debug most current request. --->
<cfset objDebug = {
CGI = Duplicate( CGI ),
URL = Duplicate( URL ),
FORM = Duplicate( FORM ),
Request = Duplicate( objRequest )
} />
<!--- Output debug to file. --->
<cfdump
var="#objDebug#"
output="#ExpandPath( './ajax_prox_debug.htm' )#"
format="HTML"
/>
--->
<!---
Get the content as a byte array (by converting it to binary,
we can echo back the appropriate length as well as use it in
the binary response stream.
--->
<cfset binResponse = ToBinary(
ToBase64( objRequest.FileContent )
) />
<!--- Echo back the response code. --->
<cfheader
statuscode="#Val( objRequest.StatusCode )#"
statustext="#ListRest( objRequest.StatusCode, ' ' )#"
/>
<!--- Echo back response legnth. --->
<cfheader
name="content-length"
value="#ArrayLen( binResponse )#"
/>
<!--- Echo back all response heaers. --->
<cfloop
item="strKey"
collection="#objRequest.ResponseHeader#">
<!--- Check to see if this header is a simple value. --->
<cfif IsSimpleValue( objRequest.ResponseHeader[ strKey ] )>
<!--- Echo back header value. --->
<cfheader
name="#strKey#"
value="#objRequest.ResponseHeader[ strKey ]#"
/>
</cfif>
</cfloop>
<!---
Echo back content with the appropriate mime type. By using
the Variable attribute, we will make sure that the content
stream is reset and ONLY the given response will be returned.
--->
<cfcontent
type="#objRequest.MimeType#"
variable="#binResponse#"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment