• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Best way to do a jQuery AJAX in JSON format calls in CFM file

Enthusiast ,
Feb 01, 2016 Feb 01, 2016

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.

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 01, 2016 Feb 01, 2016

The standard way to include a .js file.

<script type="text/javascript" src="/path/to/myJSfile.js"></script>

HTH,

^_^

Votes

Translate

Translate
LEGEND ,
Feb 01, 2016 Feb 01, 2016

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 01, 2016 Feb 01, 2016

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 01, 2016 Feb 01, 2016

Copy link to clipboard

Copied

The standard way to include a .js file.

<script type="text/javascript" src="/path/to/myJSfile.js"></script>

HTH,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 01, 2016 Feb 01, 2016

Copy link to clipboard

Copied

LATEST

Awesome! I thought I need to do something special with ColdFusion CFM. Thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation