How do I call a method in Scala through javascript & ajax?

风格不统一 提交于 2019-12-13 12:37:46

问题


I do not know if my title was perhaps a little misleading. But here's what I really need help with.

I'm making a get on this url:

$.get("/fb/login/"+fbEmail, function(data){
   console.log(data);
});

This is my routes:

GET /fb/login/:email  presentation.controllers.Auth.authenticateSocialNetwork(email:String)

And here's my action:

def authenticateSocialNetwork(email:String) = Action {
   if(!editorRepo.getEditorByEmail(email).isEmpty){
      Redirect(routes.Profile.editorProfile).withSession(Security.username -> email)
   } else {
      Redirect(routes.Profile.initiatorProfile).withSession(Security.username -> email)
   }
}

My expectation from this is that my action gets called and fires of what's inside it. In other words, Redirecting.

But what actually happens, which is not so illogical, is that my $.get call gets a response with my redirect's.

How do I actually call my action-method, without sending a response to javascript?


Here's my function in javascript, posting this snippet for it to be more clear in our discussion in the comments above.

function addClickToLoginButtons(){
$("#loginWithFb").click(function(){
    FB.login(function(response){
        if(response.authResponse){
            FB.api('/me', function(response){
                var fbEmail = response.email;
                $.get("/fb/isRegisteredAtNetwork/"+fbEmail+"/facebook", function(data){
                    if(data == "true"){
                        if(confirm("Do you want to log with facebook-account "+fbEmail+"?")){
                                $.get("/fb/login/"+fbEmail, function(data){  *//HERE'S WHERE I WOULD WANT TO CALL MY METHOD IN SCALA*
                                    console.log(data);
                                });
                        } else {
                            console.log("try again with a different facebook-account");
                        } //end confirm else
                    } else {
                        console.log("Logged in not in database");
                    }//end get else
                });
            });
        } else {
            console.log("permission not granted");
        } // end authResponse else
    }, {scope: 'email'});
});
}

回答1:


In your action, instead of returning Redirect, return Ok(urlToBeRedirectedTo).withSession(...). Once this response received in the javascript code, do your stuff and then call window.location = urlToBeRedirectedTo;.

This will add the email to the session, and will redirect to the wanted URL.



来源:https://stackoverflow.com/questions/15612825/how-do-i-call-a-method-in-scala-through-javascript-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!