Quantcast
Channel: Adobe Community: Message List - ColdFusion
Viewing all articles
Browse latest Browse all 21760

Re: JSON.parse: unexpected non-whitespace character after JSON data

$
0
0

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>


Viewing all articles
Browse latest Browse all 21760

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>