gjosh
gjosh

Reputation: 135

JavaScript submit button to return object response from spring MVC

Below code returns me a object response:

@RequestMapping(value = "/NewLogin",method = RequestMethod.POST)
public @ResponseBody Token  getAllBooks( 
    Token token = new Token();
    token.setValue(encryptedMessage);
    return token;}

On clicking the following button on jsp page :

 <input type="button" onClick="madeAjaxCall();" value="Ajax Submit">



<script type="text/javascript">

 function madeAjaxCall(){
     $.ajax({ 
         type: "post", 
         url: "http://localhost:8011/nLiveSite/livesearch/NewLogin", 
         cache: false,
         success: function(response){ 
             $('#result').html(""); 
             var obj = response; 
                console.log(obj);
             $('#result').html("Message:- " + obj );
             }, 
         error: function(){
             alert('Error while request..'); 
        } 
    }).responseText; 
} ;
</script>

Ajax Submit button is returning me content of jsp page as response. I need only object (i.e. token) as response on button click.

Upvotes: 1

Views: 1109

Answers (2)

schomax
schomax

Reputation: 337

Well, you are expecting a HTTP POST request in your Rest API (besides the typos), however you are setting the Request type to "GET" in your AJAX request. Furthermore, the URL in your request doesn't match to "/NewLogin".

Upvotes: 0

JP Dutta
JP Dutta

Reputation: 123

Do like this.....@url

url:"${pageContext.request.contextPath}/NewLogin"

Upvotes: 1

Related Questions