Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery not working with call to coldfusion cfc

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;
}
like image 394
eduski Avatar asked Dec 30 '12 22:12

eduski


2 Answers

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

like image 180
BKK Avatar answered Oct 27 '22 13:10

BKK


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.

like image 41
Abhisek Das Avatar answered Oct 27 '22 13:10

Abhisek Das