我不知道我的标题是否有点误导。但这是我真正需要帮助的。
我正在获取此网址:
$.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)
}
}
我对此的期望是我的 Action 被调用并激发其中的内容。换句话说,重定向。
但实际发生的情况(这并不是那么不合逻辑)是我的 $.get 调用通过我的重定向获得了响应。
如何在不发送 JavaScript 响应的情况下实际调用我的操作方法?
这是我在 javascript 中的函数,发布此代码片段是为了在上面的评论中我们的讨论中更加清晰。
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'});
});
}
最佳答案
在您的操作中,不返回 Redirect,而是返回 Ok(urlToBeRedirectedTo).withSession(...)
。在 JavaScript 代码中收到此响应后,执行您的操作,然后调用 window.location = urlToBeRedirectedTo;
。
这会将电子邮件添加到 session 中,并将重定向到所需的 URL。
关于ajax - 如何通过 javascript 和 ajax 调用 Scala 中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15612825/