JQuery AJAX calls - how to capture response JSON?
jQuery:
$(document).ready(function(){
$('#QuoteSearch').submit(function(){
alert("in jquery");
$.ajax({
url: "ajaxJQuery",
type: "POST",
data: {username: $("#username").val(), password: $("#password").val()},
dataType: "json",
error: function(){
alert('Error');
},
success: function(data){
alert('SUCCESS');
alert(data);
}
});
return false;
});
});
<form>
code:
<form id="Quote Search">
<textfield name="username" id="username" label="User Name" />
<textfield name="password" id="password" label="Password" />
<submit/>
</form>
<div id="coupon">
Name = <property value="name" /> and Code = <property value="code" />
</div>
Action class:
private String username;
private String password;
private String name = "Sheela";
private String code = "qwert";
public String execute() throws Exception {
System.out.println("inside execute");
name = username;
code = password;
return SUCCESS;
}
Question: How do I access the JSON returned by the Action class? I am using struts2-json-plugin, which automatically converts the Action class to JSON. I want to be able to update the div tag based on the JSON response.
This is what I see in the console:
DEBUG (org.apache.struts2.json.JSONUtil) [JSON]
{"code":"HELLO","name":"ABCD","password":"HEL开发者_StackOverflowLO","username":"ABCD"}
But I am not sure how to capture this in the JSP. Please help. gh
The JSON response from the server should be in the data
parameter in your success handler. Have you looked at what that contains?
精彩评论