-1

I want to show an alert from my CFC function after ajax post send success, anyone can help me?

this is my code:

function post(){
    $.ajax({
        type: "POST",
        URL: "myCFCName.cfc?method=save",
        data: { data1: data1, data2: data2 }, 
        success: function (message) {
            alert("success"); //it show or work fine
        }
        error: function (jqXhr, status, statusText) {
            alert("Ajax call failed: ", status, statusText);
    });
}
post("999", "133");
<!-- Coldfusion Function -->
<cffunction name="save" access="remote" returntype="JSON">
<cfargument name="data1" type="numeric" required="yes">
<cfargument name="data2" type="numeric" required="yes">
    <cfquery datasource="mydatabase" name="QuerySelect">
        SELECT * FROM TABLE
        WHERE column1 = <cfqueryparam value="#data1#" cfsqltype="CF_SQL_INTEGER">
        AND column2 = <cfqueryparam value="#data2#" cfsqltype="CF_SQL_INTEGER">
    </cfquery>
    <cfif QuerySelect.recordcount GT 0>
        <cfoutput>
            <script type="text/javascript">
                alert("data exist") //not show
            </script>
        </cfoutput>
    <cfelse>
        <cfoutput>
            <script type="text/javascript">
                alert("no record found") //not show
            </script>
        </cfoutput>
    </cfif>
</cffunction>

the console.log("success") is successfully shown in my browser, but my alert function from the CFC function not shown

  • 1
    Coldfusion is a server side language. The browser will not read that code and execute it. You would need to return valid JSON and then get javascript to create the alert using data you have returned. – haxtbh Sep 14 '20 at 11:36

1 Answers1

2

Your ColdFusion code makes no sense.

You cannot "return JavaScript and the client executes it somehow", that's not how Ajax works. You need to return data in an appropriate format (e.g. JSON), and your client code then processes that data.

Furthermore, returntype="JSON" is wrong. When you look at the network tab on your browser's console, you will see that you get error 500 from your Ajax call ("The value returned from the save function is not of type JSON"). If you use returntype the function must <cfreturn> a value of that data type. For returntype="string" the function must return a string, for returntype="numeric" it must return a number, and so on. Your function does not return anything at all.

On top of all that, JSON is not a data type in ColdFusion (unless you have defined your own JSON.cfc, which I doubt). So returntype="JSON" will never work, and especially it will not magically make your function's output JSON-compliant. What you probably mean is returnformat.

Maybe you want to return a true/false value instead.

<cffunction name="save" returntype="boolean" returnformat="JSON" access="remote">
    <cfargument name="data1" type="numeric" required="yes">
    <cfargument name="data2" type="numeric" required="yes">

    <cfquery datasource="mydatabase" name="QuerySelect">
        SELECT * 
        FROM MyTable 
        WHERE Column1 = <cfqueryparam value="#data1#" cfsqltype="CF_SQL_INTEGER">
          AND Column2 = <cfqueryparam value="#data2#" cfsqltype="CF_SQL_INTEGER">
    </cfquery>

    <cfif ExpandPath(CGI.SCRIPT_NAME) eq GetCurrentTemplatePath()>
        <cfheader name="Content-Type" value="application/json; charset=utf-8">
    </cfif>

    <cfreturn QuerySelect.RecordCount gt 0>
</cffunction>

This function returns a Boolean value in the JSON format (instead of the WDDX format, which is ColdFusion's default) and additionally it sets the Content-Type header correctly for Ajax calls, i.e. when the requested file (CGI.SCRIPT_NAME) is the current file.

This way you can use the same function

  • remotely from the client via Ajax

    function post(data1, data2){
        return $.ajax({
            type: "POST",
            url: "YourCfcName.cfc?method=save",
            data: { data1: data1, data2: data2 },
            success: function (data) {
                console.log('server returned: ', data);
            },
            error: function (jqXhr, status, statusText) {
                console.log('Ajax call failed: ', status, statusText);
            }
        });
    };
    
    post("999", "133");
    

    which results in a boolean value

  • and directly from other ColdFusion code

    <cfset thatComponent = CreateObject("component", "YourCfcName")>
    
    <cfset result = thatComponent.save("999", "133")>`
    

    which also results in a boolean value.

Miguel-F
  • 13,450
  • 6
  • 38
  • 63
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • thanks for your answer brother, so can I summarize that ColdFusion can't show an alert if I send a request by ajax with type post? – Shofan Amirudin Sep 15 '20 at 03:49
  • @ShofanAmirudin No, that's not what I said. You need to read my answer again. – Tomalak Sep 15 '20 at 06:13
  • I have read your answer, your assumptions that I want to return boolean, but what I want is to show alert Javascript from my Function (CFC), so what can I do? – Shofan Amirudin Sep 18 '20 at 08:20
  • @ShofanAmirudin You have not read my answer well enough. What does my JS code do? – Tomalak Sep 19 '20 at 17:09
  • just for calling the function CFC with post ajax, so which parts of the code/answers that you provide a function to display alertsfrom the CFC file? – Shofan Amirudin Sep 22 '20 at 01:16
  • @ShofanAmirudin Ok, again. Have you read my JS code? Really read it. Do you understand what it does? Have you *tried* it? If you're unsure, have you read the documentation for `$.ajax()` and `console.log()`? I don't know how else to put it. Read my code line-by-line. Understand what each line does. It's 10 lines of code or so, and you've been telling me "but where is the alert" for over a week now. I would think that a week is more than enough time to figure out 10 lines of code. Bottom line: I won't give you copy-and-paste code that you don't need to understand before using it. – Tomalak Sep 22 '20 at 12:35