0

My goal is to call a JSF managed bean method from inside a JS function.

This is the function:

var fbLogin = function() {
                FB.login( 
                    function(response) {
                        if (response.authResponse) {
                            var access_token =   FB.getAuthResponse()['accessToken'];
                            console.log(access_token);
                            //document.getElementById("hiddenFbForm:hiddenFbToken").value = access_token;
                            //document.getElementById("hiddenFbForm:hiddenFbLoginSubmit").onclick();
                        } else {
                            console.log("ERROR");
                        }
                    }, 
                    {scope:'email'}
                );
            };

My idea was having a hidden JSF ajax form, filling a hidden field and triggering click on submit commandlink:

<h:form id="hiddenFbForm">
            <h:inputHidden id="hiddenFbToken" value="#{loginController.fbToken}" />
            <h:commandLink id="hiddenFbLoginSubmit" actionListener="#{loginController.printFbInfo()}" />
        </h:form>

However, this doesn't work. Page reloads, my printFbInfo server side method is never reached.

Do you have better ideas for calling a JSF server side bean from a JS function (programmatically, I don't want my user to click a button).

Another thing... I want the call to be AJAX, not reloading the page.

3
  • Are you willing to use Primefaces?? Commented Sep 17, 2013 at 8:05
  • Why don't you use direct action like following: <h:commandLink id="hiddenFbLoginSubmit" action="#{loginController.printFbInfo}" /> Commented Sep 17, 2013 at 8:14
  • Yes I'm using Primefaces too. I wrote I don't want a direct action because 1) I don't want a user click but a programmatic click 2) I want an ajax call to my server method, no page reload Commented Sep 17, 2013 at 8:20

1 Answer 1

1

Try Removing Braces from Action listener method
instead of #{loginController.printFbInfo()} use #{loginController.printFbInfo}.

Since you are using Primefaces you could use Primefaces RemoteCommand (p:remoteCommand) to achieve this.

Sign up to request clarification or add additional context in comments.

1 Comment

Primefaces remote command was the way to go. Thank you. I wonder how this could be done without Primefaces however. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.