0
<html>
<body>
 <form action="login.do" method="post">
 //.....
 <input type="submit" value ="send">
 </form>
</body>
</html>

In my servlet I will be handle the request and send the json response back. How can I get the json object from the response ?

But we can do this by calling a function when we click the button.

function(){
 $ajax(
 url:"login.do"
 success: function(data){
 //..... 
 }
 )
}

is there any way to do this? Or only using the function call we can do it?

asked Jun 28, 2012 at 5:04
1
  • your json string is probably in the data parameter of the success function. use dataType : "json" in your $.ajax() call. Commented Jun 28, 2012 at 5:10

2 Answers 2

2

first argument of success callback that you call data is the json object so long as you include dataType:'json' in ajax options or use shorthand $.post(url[,data][,function(json){}),'json']) method

Read about success callback in $.ajax API :

http://api.jquery.com/jQuery.ajax/

EDIT: Using deffered method

var ajaxCall= $.post( url, dataToServer,'json')
 $.when( ajaxCall).then(function(data){
 var json =data;
 })
answered Jun 28, 2012 at 5:11
Sign up to request clarification or add additional context in comments.

Comments

2

You could set the dataType to json, or you could use the short cut method $.getJSON().

$.getJSON(your_url, function(data) {
 // data here is already an object.
 console.log(data);
});

EDIT: getJSON will use GET request type, for POST, you could do

$.post(your_url, function(data) {
 // data here is already an object.
 console.log(data);
}, 'json');
answered Jun 28, 2012 at 5:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.