November 4, 2020
How to use the fetch api with a .cfm file?
Comments
(2)
November 4, 2020
How to use the fetch api with a .cfm file?
Newbie 1 posts
Followers: 1 people
(2)

I’m trying to post a checkin.cfm file from within my index.cfm using the fetch api. All I’m getting is a 500 Server Error. Is there something different I need to do with ColdFusion?

JavaScript

fetch("./src/checkin.cfm")
    .then(function (response) {
        return response.text();
    })
    .then(function (body) {
        document.querySelector("#checkin").innerHTML = body;
    });

index.cfm

<cfoutput>
  <div id="checkin"></div>

  <script src="js/app.js"></script>
</cfoutput>

checkin.cfm

<cfoutput>

    <cfset session_valid =  application.lib.check_session_valid()>

    <h1>Hello World</h1>
</cfoutput>

Is there something wrong with my syntax? When I do this with an HTML page, it works fine.

2 Comments
2020-11-05 19:09:37
2020-11-05 19:09:37

I’d start with something even more fundamental: do you even know that the CF page is being executed? You could put a CFLOG statement (or writelog in cfscript) to confirm. (You could also remove that line that Mike wondered may be failing, though I think that would be a runtime error, so the log would still be written to before the error happened.)

Second, I notice that you say to fetch “./src/checkin.cfm”. So are you saying that the folder from which you are running the CFML page has a subfolder called src? You may want to even try simply adding that path and file to the one you’re using to call the index.cfm. Does that work at all, forgetting about the js fetch calling it? If not, that’s the problem to be fixed.

And FWIW, you may want to check if the 500 error is even coming from CF. Have you looked at the application.log for CF to see if it reports it?

Like
(1)
2020-11-05 18:45:40
2020-11-05 18:45:40

I don’t believe fetch includes cookies by default thus session info won’t be available to CF and most likely causing the 500 error when you try to verify the session. Take a look at this link, it may point you in the right direction.  https://stackoverflow.com/questions/34558264/fetch-api-with-cookie

Like
()
Add Comment