我即将使用Google OAuth实施登录。我跟着official Google docs 客户端确实从Google服务器获取令牌。但每当我尝试将其发送回我的服务器来存储它时,我似乎都没有从AJAX调用中获取数据。这是我的代码:
function signInCallback(authResult) { //called when client has confirmed the Login dialog
if (authResult['code']) {
// Hide the sign-in button now that the user is authorized, for example:
$('#signinButton').attr('style', 'display: none');
console.log("signinCallback. Send code to server:")
console.log("Code = "+ authResult['code']);
console.log("performing AJAX call now...\n\n")
// Send the code to the server
$.ajax({
type: 'POST',
url: 'http://localhost:9000/storeauthcode',
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
// Handle or verify the server response.
console.log("AJAX call --> success");
console.log(result)
},
processData: false,
data: authResult['code']
});
} else {
//There was an error.
console.log("AJAX call failed.")
}
我在路线文件中创建了一行:
POST /storeauthcode controllers.Application.storeAuthCode
服务器上的方法:
def storeAuthCode = Action { request =>
Ok("storeauthcode called with queryString = "+request.rawQueryString)
}
以下是我的Chrome浏览器的控制台输出:
hello.js:2 Welcome to your Play application's JavaScript!
(index):67 signinCallback. Send code to server:
(index):68 Code = <token deleted for privacy reasons>
(index):69 performing AJAX call now...
(index):77 AJAX call --> success
(index):78 storeauthcode called with queryString =
因此,虽然我的浏览器获得了令牌,但我无法将其存储在服务器上,因为我没有收到任何数据。
Ok("storeauthcode called with queryString = "+request.body.asJson)
也是空的。
这里的结果也是空洞的结果:
def storeAuthCode = Action { request =>
Ok("storeauthcode called with queryString = "+request.getQueryString("data"))
}