1

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.

gjosh
  • 135
  • 3
  • 18

2 Answers2

1

Do like this.....@url

url:"${pageContext.request.contextPath}/NewLogin"
JP Dutta
  • 123
  • 1
  • 13
0

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".

schomax
  • 337
  • 2
  • 9
  • hi @schomax find the url "~/nLiveSite/NewLogin/add.htm?_=1375182146256" Above example is with post but if I try with another get method still URL is getting appended with some value as shown here. – gjosh Jul 30 '13 at 11:03
  • You can set both requests to type GET. If you want to append parameters it can be done with the option "data" within the ajax method, for example: data: {value: 1} – schomax Jul 30 '13 at 14:29
  • Furthermore, you need to make sure that the service is hosted within the same domain as your web application. Otherwise you run into the "same origin policy" constraint, where you would need to use JSONP. That is further well explained here http://stackoverflow.com/a/11736771/2179109 – schomax Jul 30 '13 at 14:38
  • And this is how to modify server and client for using JSONP, which worked for me http://stackoverflow.com/a/10328288/2179109 – schomax Jul 30 '13 at 14:44