Copy link to clipboard
Copied
I am having problem using cold fusion and jquery.ajax it will throw error
JSON.parse: unexpected non-whitespace character after JSON data
this is the response in firebug {"EMPCODE":"E-00001"}
child.cfm
<cfif IsDefined("empmycode")>
<cfset myarray= getempCode(#mycode#)>
<cfoutput>#myarray#</cfoutput>
</cfif>
<cffunction name="getempCode">
<cfargument name="empcode">
<cfquery name="empQuery" datasource="#datasource#">
Select empcode from employee where empcode = '#empcode#'
</cfquery>
<cfset mystruct = StructNew()>
<cfset mystruct.empcode=#empQuery.empcode#>
<cfreturn SerializeJSON(mystruct)>
</cffunction>
parent.cfm
$.ajax({
type: 'post',
data: {empmycode:empcode},
url: 'child.cfm',
success:function(data){
var myobjc = jQuery.parseJSON(data);
console.log(myobj.empcode);
}
});
Thank you in advance
Copy link to clipboard
Copied
The JSON fragment you provided looks OK, but I'm guessing that something else is at play here. Is that all that you see in Firebug? Is there stuff further down the console if you scroll?
Do you have request debugging turned on in your application? If so, that will append a bunch of stuff to the response from child.cfm. Rather than use child.cfm. You can add <cfsetting showDebugOutput="no"> to the top of your page to suppress that. You might also want to move the function definition to be above the <cfif> block, and add <cfprocessingdirective suppressWhiteSpace = "yes"> to the top of the page and </processingdirective> to the bottom. You could also put <cfcontent reset="yes"> on the same line and immediately preceeding your <cfoutput> block.
I would also suggest you use <cfqueryparam> in your <cfquery> block to protect against potential SQL injection from the 'empcode' form variable (coming in from your AJAX POST call).
You might consider refactoring the child.cfm into a remote CFC. You could assign returntype="JSON" on the getempCode() function, which would eliminate the need for the SerializeJSON() call in your <cfreturn>.
Another thing - is the empCode supposed to be returned in scientific notation (or at least that is what "E-00001" appears to be)? If not, you might be falling prey to one of the known issues with SerializeJSON().
-Carl V.
Copy link to clipboard
Copied
jemz wrote:
<cfif IsDefined("empmycode")>
<cfset myarray= getempCode(#mycode#)>
<cfoutput>#myarray#</cfoutput>
</cfif>
<cffunction name="getempCode">
<cfargument name="empcode">
<cfquery name="empQuery" datasource="#datasource#">
Select empcode from employee where empcode = '#empcode#'
</cfquery>
<cfset mystruct = StructNew()>
<cfset mystruct.empcode=#empQuery.empcode#>
<cfreturn SerializeJSON(mystruct)>
</cffunction>
The above code is confusing. You test for the existence of empmycode, yet you actually use mycode instead. In addition, what you call an array isn't, and you fail to 'var' the method's local variables.
You could modify the code, by scoping, as well as bearing in mind what Carl has said:
<cfif IsDefined("form.empmycode")>
<cfset code= getempCode(form.empmycode)>
<cfoutput>#code#</cfoutput>
</cfif>
<cffunction name="getempCode">
<cfargument name="empcode">
<cfset var mystruct = StructNew()>
<!--- Alternative: <cfqueryparam cfsqltype="cf_sql_varchar" value="'#arguments.empcode#"> --->
<cfquery name="empQuery" datasource="#datasource#">
Select empcode from employee where empcode = <cfqueryparam cfsqltype="cf_sql_integer" value="'#arguments.empcode#">
</cfquery>
<cfset mystruct.empcode=empQuery.empcode>
<cfreturn SerializeJSON(mystruct)>
</cffunction>