如何通过javascript&在Scala中调用方法阿贾克斯? [英] How do I call a method in Scala through javascript & ajax?

查看:92
本文介绍了如何通过javascript&在Scala中调用方法阿贾克斯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我的头衔是否可能有点误导.但是,这正是我真正需要帮助的地方.

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

我正在使用此URL:

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

这是我的路线:

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

这是我的动作:

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.

但是实际发生的事情,不是很不合逻辑,是我的$ .get调用获得了重定向请求的响应.

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

我实际上如何调用我的操作方法,而不发送对javascript的响应?

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

这是我使用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'});
});
}

推荐答案

在您的操作中,返回Ok(urlToBeRedirectedTo).withSession(...)而不是返回Redirect.在javascript代码中收到此响应后,请处理您的工作,然后调用window.location = urlToBeRedirectedTo;.

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;.

这会将电子邮件添加到会话中,并将重定向到所需的URL.

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

这篇关于如何通过javascript&在Scala中调用方法阿贾克斯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆