Copy link to clipboard
Copied
Instead of using cfhttp in my CFM file, I like to use jQuery AJAX in JSON to do the query. Should I just simply putting the JS script on the CFM page? Or, do all of the work in a .js file and calling this .js file in the CFM page to display and formatting? Or should I just use the .js file to make the JSON calls and also targeting the IDs in the CFM file for display purposes?
Thank you.
1 Correct answer
The standard way to include a .js file.
<script type="text/javascript" src="/path/to/myJSfile.js"></script>
HTH,
^_^
Copy link to clipboard
Copied
It depends upon whether or not you're going to be submitting CF data as part of the AJaX call.
Personally, I use inline JavaScript/jQuery, just in case I need to do anything regarding CF variables, etc. But if you don't need to pass anything CF generated to the AJaX, then a .js file will work, fine.
var thisForm = $('#formName').serializeArray();
$.ajax({
type: "POST",
url: "/path/to/function.cfc?method=myFunction",
data: {formData : thisForm},
contentType: "JSON"
}).done(function(data){
var datab = JSON.parse(data);
... //whatever you need to do with the returned data
});
HTH,
^_^
Copy link to clipboard
Copied
Thanks! That is very helpful. No, I don't need to pass anything. I just need to do a get. So, if I do an external .js file, how to I reference that file in .cfm file?
Copy link to clipboard
Copied
The standard way to include a .js file.
<script type="text/javascript" src="/path/to/myJSfile.js"></script>
HTH,
^_^
Copy link to clipboard
Copied
Awesome! I thought I need to do something special with ColdFusion CFM. Thanks again!

