2

I am currently writing a lightning component and came across this,

ComponentController.js

someFunction: function(component) {
    var action = component.get("c.apexFunction");
    action.setParams({
        // Params
    });
    action.setCallback(this, function(response) {
       console.log(response);
       // Response handling
    });
    action.enqueueAction(action);
}

apexClass.apxc

public class apexClass {
    @AuraEnabled
    public static String apexFunction(String params) {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(apiUrl);
        req.setMethod('POST');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        String payLoad = 'payload';
        req.setBody(payLoad);
        HttpResponse res = h.send(req);
        return res.getBody();
    }
}

So when the function is called the first time the response is printed out but the next time the function is called, the response is not printed out(the function gets called though.

5
  • FYI, one cause of a callback not happening.
    – Keith C
    May 13 '18 at 14:20
  • can you post your calling code?
    – MandyKool
    May 14 '18 at 2:37
  • Where were you run the code? On LEXPage or a Standalone app?
    – am_Aravi
    May 14 '18 at 7:25
  • @KeithC atleast the post request should execute, right? I mean while debugging I don't see any post request being executed in the networks tab in chrome console
    – Anto Joy
    May 14 '18 at 9:47
  • @AntoJoy I'm not sure; the post may not be done immediately because the framework tries to group multiple requests into one request.
    – Keith C
    May 14 '18 at 11:04
1

Your Apex class method does not have AuraEnabled annotation.

It is needed for lightning.

public class apexClass {
    @AuraEnabled 
    public static String apexFunction(String params) {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(apiUrl);
        req.setMethod('POST');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        String payLoad = 'payload';
        req.setBody(payLoad);
        HttpResponse res = h.send(req);
        return res.getBody();
    }
}

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_apex.htm

5
  • Oops sorry it has I just missed it out while copy pasting
    – Anto Joy
    May 14 '18 at 9:48
  • Can you check debug logs to see is aura method is even called? is it the real code or pseudo code May 14 '18 at 9:51
  • First time it is being called ... a log is generated and also i can see the request was successful from the Networks tab in chrome. The second time and onwards, the function is invoked no logs are generated and neither is a post request (no request in the networks tab). Its a pseudo code
    – Anto Joy
    May 14 '18 at 9:59
  • Was that component dynamically created? Is that component inside aura:if section? When you call the method 1st time , after parsing the response are you doing any change to the same or other component? May 14 '18 at 10:05
  • the component is not dynamically created. I havent used any aura:if section. I am only appending another div to the component on getting the response
    – Anto Joy
    May 14 '18 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.