Summary
I have an application that performs a search. Before allowing the submission, it sends an AJAX request to the request to check for a valid zip code, and then returns a JSON result that I can parse. I need to do the same in the cross domain, and I know that I should use the full URL and JSONP format instead, but I'm not sure how to set it.
AJAX call
I am sending a zip code that is launched through a request.
if (zipLength == 5) {
$.ajax({
type:"GET",
url: "cfc/test.cfc",
data: {method:'zip_lookup', zip:zip},
success: function(obj) {
var response = $.parseJSON(obj);
if (response.formError == true) {
alert(response.message);
}
}
});
}
CFC in Coldfusion that launches a request
<cffunction name="zip_lookup" access="remote">
<cfquery name="qZip">
Select Distinct ZipCode
From zipcodes
Where ZipCode = '#url.zip#'
</cfquery>
<cfif qZip.RecordCount EQ 0>
<cfset formError = true>
<cfset message = "Invalid Zip">
<cfelse>
<cfset formError = false>
<cfset message = "">
</cfif>
<cfoutput>
<cfset obj =
{
"formError" = formError,
"message" = message
}
/>
</cfoutput>
<cfprocessingdirective suppresswhitespace="Yes">
<cfoutput>
#serializeJSON(obj)#
</cfoutput>
</cfprocessingdirective>
<cfsetting enablecfoutputonly="No" showdebugoutput="No">
</cffunction>
JSON response
This is the result of a request.
{"message":"Invalid Zip","formError":true}
AJAX, formError message JSON. JSONP?
success: function(obj) {
var response = $.parseJSON(obj);
if (response.formError == true) {
alert(response.message);
}
}