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.

6
  • what is the response content-type and request content-type as seen in firebug? Commented Jul 30, 2013 at 7:39
  • it should be url: "http://localhost:8011/NewLogin/", ? Commented Jul 30, 2013 at 9:25
  • @DanielRobertus problem is not with URL hit as bying Writing alert in Success it is getting called and response shows full jsp page instead of response object. Commented Jul 30, 2013 at 10:10
  • @rps I am using headers ="application/json" in controller class. Commented Jul 30, 2013 at 10:11
  • and java script has the following: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> Commented Jul 30, 2013 at 10:21

2 Answers 2

1

Do like this.....@url

url:"${pageContext.request.contextPath}/NewLogin"
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

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.
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}
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 stackoverflow.com/a/11736771/2179109
And this is how to modify server and client for using JSONP, which worked for me stackoverflow.com/a/10328288/2179109

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.