5

The following javascript code works with the facebook login window appearing and allowing a user to login. The response values are captured and I know it works as alerts appear where setup but I cannot pass the value back to a controller method.

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

     System.out.println(token);

     return token;
  } 

Javascript method called:

    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});
     };

The error I get is the following:

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

Appreciate responses.

7
  • is your controller annotated with @RequestMapping("/HelloController") ? Commented Dec 7, 2015 at 20:58
  • @RequestMapping(value ="/HelloController" , method =RequestMethod.POST) public String getAccessToken( Commented Dec 7, 2015 at 23:01
  • 1
    Can you post your entire Controller class? Commented Dec 8, 2015 at 12:38
  • 1
    Please add the controller definition Commented Dec 8, 2015 at 13:12
  • 1
    Could you also post your Spring dispatch configuration? Are you using any url pattern such as *.html? Commented Dec 10, 2015 at 21:12

1 Answer 1

2
+25

The problem could be that you are using a new version of JQuery that sends request data as post form data instead of JSON as default. Try changing your ajax call to the following. The form data would not be recognized by your controller so if this is the case you should see a 404.

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

For reference see this post: Send JSON data via POST (ajax) and receive json response from Controller (MVC)

Sign up to request clarification or add additional context in comments.

6 Comments

Did this help you? Please accept the answer if it did.
Sorry for the late follow up & I did eventually figure it out but will accept your answer. Thanks
I'm sorry; I revisited this program and still getting the same error.
Please provide some more info. This solved the problem for me in a similar setup.
sorry Pablo. I found the issue and it was the controller mapping value that needed to match the url call in the ajax function. thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.