2015-12-01 39 views
5

以下JavaScript代碼與出現的Facebook登錄窗口一起使用,並允許用戶登錄。響應值被捕獲,我知道它工作,因爲警報出現在設置的位置,但我無法將值傳遞迴控制器方法。傳遞JavaScript響應變量到彈簧控制器功能

@RequestMapping(value ="/getAccessToken" , method = RequestMethod.POST) 
public @ResponseBody String getAccessToken(@RequestBody String token){ 

    System.out.println(token); 

    return token; 
    } 

Javascript方法叫:

function doLogin() { 
     FB.login(function(response) { 
     alert(response); 
     console.log(response); 
     if (response.authResponse) { 
        alert(response.authResponse.userID); 
        alert(response.authResponse.accessToken); 
        var Token = response.authResponse.accessToken; 
        alert(Token); 
        $.ajax({ 
         type: "POST", 
         url: "/HelloController/getAccessToken", 
         data: Token, 
         success: function (result) { 
           alert("Token"); 
         }, 
         error: function (result) { 
           alert("oops"); 
         } 
        }); 
        document.getElementById('loginBtn').style. 
     display = 'none'; 
     getUserData(); 
     }}, {perms:'manage_pages', 
     scope: 'email,public_profile', return_scopes: true}); 
    }; 

我得到的錯誤是:

WARN 25660 --- [nio-8080-exec-9] 
o.s.web.servlet.PageNotFound    : 
Request method 'POST' not supported 

欣賞迴應。

+0

是你用@RequestMapping(「/ HelloController中標註的控制器「)? – reos

+0

@RequestMapping(value =「/ HelloController」,method = RequestMethod.POST)public String getAccessToken( – vbNewbie

+1

)您可以發佈您的整個控制器類嗎? – Mohit

回答

2

問題可能在於您正在使用新版本的JQuery,它將請求數據作爲發佈表單數據而不是JSON發送爲默認值。嘗試改變你的ajax調用以下。表單數據不會被你的控制器識別,所以如果是這樣的話,你應該可以看到一個404

$.ajax({ 
     type: "POST", 
     traditional: true, 
     url: "/HelloController/getAccessToken", 
     data: JSON.stringify(Token), 
     success: function (result) { 
      alert("Token"); 
     }, 
     error: function (result) { 
      alert("oops"); 
     } 
     }); 

僅供參考看到這個帖子:Send JSON data via POST (ajax) and receive json response from Controller (MVC)

+0

這對你有幫助嗎?請接受答案,如果有。 –

+0

抱歉後期跟進&我終於搞清楚了,但會接受你的答案。謝謝 – vbNewbie

+0

對不起,我重新訪問了這個程序,仍然得到同樣的錯誤 – vbNewbie

相關問題