2

I would like to call a cfc function that I created that will take an input, and based on that input, mark a bit field on a MSSQL database. It's something I'm not too familiar with, but looks something like this.

Agendalist CFC

<cffunction name="iTalked">
    <cfargument name="SpeakerName" required="true" type="string">
        <cfquery name="iTalked" datasource="SpeakerCard">
            UPDATE tbl_SpeakerCard_Log
            SET Spoken = 1
            WHERE SpeakerName = '#SpeakerName#'
        </cfquery>
    <cfreturn iTalked>
</cffunction>

AJAX Call

function HasSpoken(name)
    {
        $.AJAX('actions.AgendaList.cfc?wsdl', {method : 'iTalked', SpeakerName: name});
    }

I'm pretty sure my Ajax call is incorrect in some form, but I'm not well versed enough to understand what the error is. if you can offer any help I'd appreciate it. TIA!

khgove
  • 69
  • 8
  • 1
    One thing, add access="remote" to the function. – Evik James Jul 07 '15 at 22:03
  • 3
    the cffunction needs access attribute set to remote; the call don't need wsdl argument. For further hints look at: http://www.bennadel.com/blog/1887-using-jquery-to-pass-arrays-to-remote-coldfusion-components.htm – Bernhard Döbler Jul 07 '15 at 22:04
  • Added access="remote" to the function and removed the wsdl portion of the cfc call, but the function still isn't updating in the DB. I'll be reading Ben Nadel's blog that you linked in the meantime and hopefully I'll find my answer there. – khgove Jul 07 '15 at 22:20
  • 2
    AJAX requests are HTTP requests, you need to give it *a URL*, not a dotted path (which is meaningless to a browser). Also you're opening up yourself for SQL injection by hard-coding your parameter values into your SQL string, rather than passing them as actual parameters. – Adam Cameron Jul 07 '15 at 22:39
  • 1
    One step at a time. First, make sure your query works without using ColdFusion. Then, make sure it works from a .cfm page. Then make sure it works from a cfc called from a .cfm page. Only when that all works do you call it from javascript. This way, you can isolate the problems and cope with them one by one. Also, your query, as written, will fail if the name contains an apostrophe. This is one of many reasons you should use query parameters. – Dan Bracuk Jul 07 '15 at 23:33
  • 1
    Agreed you are trying to do too much at once. Before you even think about calling a remote function from javascript, verify it works when invoked from a browser. ie `http://yourserver.com/path/to/AgendaList.cfc?method=iTalked&SpeakerName={someTestValueHere}`. The above will *not* work, even from CF. UPDATE queries do not return a resultset, and as mentioned, it is not "remote" accessible. Also, though these things will not cause an error you should a) always use cfqueryparam on variable parameters b) localize query objects and c) scope variable names ie `arguments.SpeakerName` – Leigh Jul 08 '15 at 01:43
  • 1
    Once you have verified it works from CF, then add jQuery to the mix. Couple things to remember: javascript is case sensitive so `$.AJAX` != `$.ajax`. Also, get familiar with your browser's debugging tools so you will be able see any javascript errors that occur on the jQuery side. – Leigh Jul 08 '15 at 01:45
  • Also consider using cfqueryparam on your query. You leave yourself open to SQL injection the way your query is written. – Sam M Jul 08 '15 at 15:27

0 Answers0