Skip to main content
2Charlie
Inspiring
February 1, 2016
Answered

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

  • February 1, 2016
  • 1 reply
  • 1636 views

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.

    This topic has been closed for replies.
    Correct answer WolfShade

    The standard way to include a .js file.

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

    HTH,

    ^_^

    1 reply

    WolfShade
    Legend
    February 1, 2016

    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,

    ^_^

    2Charlie
    2CharlieAuthor
    Inspiring
    February 1, 2016

    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?

    WolfShade
    WolfShadeCorrect answer
    Legend
    February 1, 2016

    The standard way to include a .js file.

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

    HTH,

    ^_^