You need to Serialise your data in the ‘CFML’ template:
checkin.cfm
OPTION 1:
<cfset data = StructNew()>
<cfset data['session_valid'] = application.lib.check_session_valid()>
<cfset data['html'] = '<h1>Hello World</h1>'>
<cfoutput>
#SerializeJSON(data)#
</cfoutput>
OPTION 2:
<cfset data = StructNew()>
<cfset data['session_valid'] = application.lib.check_session_valid()>
<cfsavecontent variable="html">
<h1>I am a whole page of HTML</h1>
</cfsavecontent>
<cfset data['html'] = html>
<cfoutput>
#SerializeJSON(data)#
</cfoutput>
Also, please make sure that you have set the correct:
Access-Control-Allow-Origin
If your client side code is on a different server/port to your CFML template. In production, this probably isn't an issue, but, if like me, you use Angular, which runs locally on port 4200, and your Coldfusion Application Server runs on port 8500, then this header becomes very important.
In your JavaScript, you need to parse the JSON response:
fetch("./src/checkin.cfm")
.then(function (response) {
return response.text();
})
.then(function (json) {
var response = JSON.parse(json);
console.log('response: ',response);
if('html' in response){
document.querySelector("#checkin").innerHTML = response['html'];
}
});
I have tested this, and it works 100%, so if you are still getting an error, I would suggest you look at the response using:
Firefox Dev Tools -> Network -> XHR -> Response
And see if Coldfusion is throwing an error in checkin.cfm, due to a problem with:
application.lib.check_session_valid()