Jquery not working with call to coldfusion cfc
Asked Answered
L

2

5

I am new to jquery and am trying to make functions that will call and update my database with a dialog box. I modified an existing template to produce the code below and can't get the function savefee to be called by the jquery function. There are no errors in my javascript console. Any help is appreciated.

`

<cfset getfees = new artservice().getfees()>

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>

<script>
$(document).ready(function() {

    $(".artdiv").click(function() {
        var initialDiv = this;

        //based on which we click, get the current values
        var feeid = $(this).data("fee_id");
        var feetitle = $("h2", this).text();

        // set form values
        $("#fee_id").val(feeid);        
        $("#fee_title").val(feetitle);

        $("#editForm").dialog({
            buttons: {
                "Save": function() {
                    var thisDialog = $(this);
                        $.post("artservice.cfc", {
                        method: 'savefee',
                        fee_id: $("#fee_id").val(),
                        fee_title: $("#fee_title").val()
                        }, 
                    function() {
                        //update the initial div
                        $("h2", initialDiv).text($("#fee_title").val());
                        $(thisDialog).dialog("close");
                    });
                }
            }
        });
    });

});
</script>
<style>
.artdiv {
    padding: 5px;
    margin: 5px;
    background-color: #80ff80;
}
#editForm {
    display:none;   
}
</style>
</head>

<body>

<cfoutput query="getfees">

    <div class="artdiv" data-fee_id="#fee_id#">
        <h2>#fee_title#</h2>
    </div>

</cfoutput>

<div id="editForm" title="Edit Art">
    <input type="hidden" id="fee_id">
    <p>
    <b>Name:</b><br/>
    <input type="text" id="fee_title">
    </p>

</div>

</body>
</html>

`

The cfc is below

`

<cffunction name="getfees" access="public">
    <cfquery datasource="dsn" name="getfees" maxrows="10">select fee_id, fee_title from table</cfquery>
    <cfreturn getfees>
</cffunction>

<cffunction name="savefee" access="public">
<cfargument name="fee_id" required="yes">
<cfargument name="fee_title" required="yes">
<cfquery datasource="dsn">update table set fee_title = '#arguments.fee_title#' where fee_id = #fee_id#</cfquery>
</cffunction>

`

error was with this function in application.cfc "The ARGS argument passed to the onCFCRequest function is not of type string"

public void function onCFCRequest(required string cfcname, required string method, required string args) {

    return;
}
Lemur answered 30/12, 2012 at 22:10 Comment(5)
inspect AJAX request in browser console...is it being made? Is status 200? correct data sent ?...doesn't appear to be any cfoutput...should there be? Console will help narrow down server vs client problemsDemetra
Yes AJAX request is being made. Status is 200. I get an error on the response "The ARGS argument passed to the onCFCRequest function is not of type string" which was in my application.cfc file (separate from this one). Code is below. I commented out the function and it is now working fine. Ideas why this happened. ` public void function onCFCRequest(required string cfcname, required string method, required string args) { return; } `Lemur
suggest updating question with error... personally I am more front end, coldfusion, particularly CFC, is not my strong point. Can write a mean <cfdump> tag though!....handy for ajax sometimesDemetra
Once you get the save fee function to work, you'll want to display such to the user. Looks like you haven't got to that part yet.Corabella
You have a potential ambiguity. Your getFees query returns up to 10 records, all of which can have different values of fee_title. I'm not convinced that "var feetitle = $("h2", this).text();" is always going to pass the correct value. You'll have to look at this once you get the function to actually execute.Corabella
S
4

The the error you are getting because of the incorrect function definition of 'onCFCRequest'. The third argument of this function is a structure type but you have made the type as string. So the proper definition should be like.

public void function onCFCRequest(required string cfcname, required string method, required struct args) {

    return;
}

Also, one more thing if you declare this function in the Application.cfc then you need to manually handled the function call inside the 'onCFCRequest'. Something like you need to again recall the request function inside onCFCRequest.

Sabbath answered 31/12, 2012 at 6:56 Comment(3)
Two follow up questions - 1) Is declaring this function even necessary ? 2) your last two sentences lost me -- can you provide an example?Lemur
@Lemur - onCFCRequest is invoked instead of the requested cfc method. Since your onCFCRequest function is empty, it will intercept requests for cfc's, then do .. nothing. If you want to automatically invoke the requested cfc, you must add code for that inside onCFCRequest. See the full example at the bottom of the docs . (Unfortunately the code is not dynamic, but does demonstrate the concept).Dick
@eduski- Answer to your questions. 1) It depends on you how you want to use it, if you implements 'onCFCRequest' then you have more control over Ajax, Web Service, and Flash Remoting requests to your CFC. It can be used as a extra security layer on the top of you CFC. If you do not want to use this function then you can do that no big deal. 2) I think Leigh have already answer to that question. Thanks to Leigh.Sabbath
B
5

Your functions must be set to access="remote" since they are being called by the browser directly to the CFC.

Beaudette answered 30/12, 2012 at 23:2 Comment(4)
Only the second one actually. The getFees function is being called with ColdFusion code which is the first line displayed.Corabella
Should really have cfcomponent as a container as well. Otherwise this is a list of functions.Antoninus
And parameterise your queries rather than pass data straight from the URL into your DB! That's a recipe for disaster. Oh: and VAR your variables in your functions.Brainstorming
understood. was just trying a proof of concept so the code was simplified, not meant to go live. thanks for all of your suggestions. The error I was getting is posted above (edit) from a different (paypal) application.Lemur
S
4

The the error you are getting because of the incorrect function definition of 'onCFCRequest'. The third argument of this function is a structure type but you have made the type as string. So the proper definition should be like.

public void function onCFCRequest(required string cfcname, required string method, required struct args) {

    return;
}

Also, one more thing if you declare this function in the Application.cfc then you need to manually handled the function call inside the 'onCFCRequest'. Something like you need to again recall the request function inside onCFCRequest.

Sabbath answered 31/12, 2012 at 6:56 Comment(3)
Two follow up questions - 1) Is declaring this function even necessary ? 2) your last two sentences lost me -- can you provide an example?Lemur
@Lemur - onCFCRequest is invoked instead of the requested cfc method. Since your onCFCRequest function is empty, it will intercept requests for cfc's, then do .. nothing. If you want to automatically invoke the requested cfc, you must add code for that inside onCFCRequest. See the full example at the bottom of the docs . (Unfortunately the code is not dynamic, but does demonstrate the concept).Dick
@eduski- Answer to your questions. 1) It depends on you how you want to use it, if you implements 'onCFCRequest' then you have more control over Ajax, Web Service, and Flash Remoting requests to your CFC. It can be used as a extra security layer on the top of you CFC. If you do not want to use this function then you can do that no big deal. 2) I think Leigh have already answer to that question. Thanks to Leigh.Sabbath

© 2022 - 2024 — McMap. All rights reserved.