2

I'm building a JSON string within script Tags in a cfm file. I would like to find a way to get this JSON string into a cfoutput so that the page can display the JSON.

I'm currently just document.write()ing the json which retrieves the JSON in the browser but won't return anything with a POST request. I've tried using the toScript() coldfusion function but I think this does the opposite of what I want (puts cf var into js when I want to put a js var into cf).

I'm currently doing this

<cfoutput>
    <script type = "text/javascript"> 
        var someJSON = "{\"some\": \"data\"}";
        document.write(JSON.stringify(someJSON));
    </script>
</cfoutput>

I'm still getting to grips with coldfusion so sorry if this seems archaic. I would like to instead pass someJSON to a coldfusion variable so I can cfoutput tag so that I get a response with a POST request to this particular endpoint because currently I get no response.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • 1
    Make sure you [understand the difference between serverside and clientside](https://softwareengineering.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming/171210#171210). If you have clientside JavaScript and want to submit information to the serverside ColdFusion, you need to send them via HTTP. This can be done by either using a `
    ` submit or via `FormData`, using AJAX (XHR = XML HTTP Request).
    – Alex Aug 06 '19 at 19:48
  • 1
    *but won't return anything with a POST request* Could you elaborate on what you mean by "with a POST request"? Are you saying a different page is POST'ing data to your .cfm script, and you want to display it with cfoutput? – SOS Aug 06 '19 at 20:51
  • I wonder if the user just wants to set an `` and just POST it that way. The user could do something like this: https://stackoverflow.com/questions/2979772/set-value-of-hidden-field-in-a-form-using-jquerys-val-doesnt-work – James A Mohler Aug 06 '19 at 21:59
  • Yeah, that's possible - though the mention of " this particular endpoint" suggested the script might be *receiving* a POST. Hard to say without more clarification though ... – SOS Aug 07 '19 at 01:12
  • @Ageax Sorry maybe I don't understand rest too well. I want when someone to enter the url to the cfm file, for the page to display the json that I have constructed in javascript. I realize this seems weird but I'm using a JS library to generate mock data which is why I'm building the JSON in js. – Karim Shoorbajee Aug 07 '19 at 19:53
  • 1
    **Edit** @KarimShoorbajee - Yeah, that won't work the way you're hoping. Even though you can mix JS/CF code in the same file [they don't interact all](http://blog.adamcameron.me/2012/10/the-coldfusion-requestresponse-process.html). You can't output a javascript variable from CF because it doesn't know anything about JS, nor does JS understand CFML. Even it did, the timing makes it impossible. CF code is executed 1st. It's converted into html and returned to the web server. Then the CF server disconnects. By the time your browser get's it, the CF Server is long gone. – SOS Aug 07 '19 at 21:46
  • (Edit) *"...currently I get no response.."* Using what? Even though it may not be the correct approach ... your code will return - something. *"..maybe I don't understand rest too well."* Are you trying to create/simulate a [RESTful webservice](https://helpx.adobe.com/coldfusion/developing-applications/changes-in-coldfusion/restful-web-services-in-coldfusion.html)? – SOS Aug 08 '19 at 22:27

2 Answers2

2

To get a string out ColdFusion and into Javascript, you need to do something like this:

<cfset cfdata = {"some": "data"}>


<cfoutput>
<script type = "text/javascript"> 
    var someJSON = #serializeJSON(cfdata)#;
    document.write(JSON.stringify(someJSON));
</script>
</cfoutput>

Results

enter image description here

See: https://cffiddle.org/app/file?filepath=f9d44f3a-5711-456e-b25a-bf31fead1fa8/6677fc2a-f41b-47d7-b11a-352575f95738/4e1ecc15-ac98-450f-a7fe-b28cf395b012.cfm

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • 1
    I think he's trying to do the opposite ;-) but... despite re-reading the post twice, I'm still not entirely clear on why - or if it's really what's needed. – SOS Aug 06 '19 at 20:57
0

You have some form.cfm that is making an Ajax request to data.cfm. The data file should return JSON to the JavaScript in the form file.

The data.cfm file basically needs to do this:

<cfscript>
data = {
    a: 1,
    b: 2,
    c: 3
};

cfcontent(type="application/json; charset=UTF-8")
writeOutput(serializeJSON(data))
</cfscript>

The cfcontent function tells the browser that the content being returned is JSON. Without that, it's just a string and you'd have to do more with JavaScript to make sense of the string.

The data returned will look like

{"A":1,"B":2,"C":3}

since ColdFusion stores struct keys in uppercase. In order to make the data return as lowercase keys, you can do either

data = {};
data["a"] = 1;
data["b"] = 2;
data["c"] = 3;

or

data = {
    "a": 1,
    "b": 2,
    "c": 3
};

which will then return the JSON with lowercase keys.

{"a":1,"b":2,"c":3}
Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44