简体   繁体   中英

Not able to call apex controller from lightning component's helper class

I am creating a basic lightning component which got just one button. On click of this button, I am calling an apex method which return a string. For some reason, whenever I am clicking that button, I get no response. In the console, lightning event log and debug log, I get no error. I have no idea as whats going on and how to debug this. Please help.

I tried debugging it on the event log, debug log and the console. Not able to figure out. Please help!

            COMPONENT: 
            <aura:component controller="CustomMassDownload" implements="force:appHostable,force:hasRecordId,flexipage:availableForAllPageTypes,force:lightningQuickAction" access="global" > 

                <div class="slds-p-top_xx-large">
                       <button type="button" onclick="{!c.downloadFile}" >Download</button> 
                </div>
            </aura:component>

            CONTROLLER:
            ({
                downloadFile : function(component, event, helper) {
                    console.log('why?');
                    helper.getString(component,event,helper);
                }
            })

            HELPER:

            ({
getString : function(component,event,helper) {
    console.log('owl');
    var action = component.get("c.ReturnString");
    action.setParams({
        abc: "djflskj"
    });
    console.log('puppy' + action);
    action.setCallback(this,function(response){
        console.log('issuccess');
        var state = response.getState();
        if(state === "SUCCESS"){
            console.log('love');
        }else{
            console.log('hate');
        }
    });

}

}) APEX:

            public class CustomMassDownload{

                @AuraEnabled
                public static String ReturnString(String abc){
                system.debug('aaaaaaaaaa');
                return abc;
                }
            }

After further looking at your code I see you are not actually calling your apex controller.

You need to add $A.enqueueAction to your getString .

getString : function(component,event,helper) {
    console.log('owl');
    var action = component.get("c.ReturnString");
    action.setParams({
        abc: "djflskj"
    });
    console.log('puppy' + action);
    action.setCallback(this,function(response){
        console.log('issuccess');
        var state = response.getState();
        if(state === "SUCCESS"){
            console.log('love');
        }else{
            console.log('hate');
        }
    });
    $A.enqueueAction(action);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM