JQuery AJAX调用 - 如何捕获响应JSON?

时间:2021-06-09 14:29:45

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.

问题:如何访问Action类返回的JSON?我正在使用struts2-json-plugin,它自动将Action类转换为JSON。我希望能够根据JSON响应更新div标签。

This is what I see in the console:

这是我在控制台中看到的:

DEBUG (org.apache.struts2.json.JSONUtil) [JSON]
{"code":"HELLO","name":"ABCD","password":"HELLO","username":"ABCD"}

But I am not sure how to capture this in the JSP. Please help. gh

但我不确定如何在JSP中捕获它。请帮忙。 GH

1 个解决方案

#1


4  

The JSON response from the server should be in the data parameter in your success handler. Have you looked at what that contains?

来自服务器的JSON响应应该在成功处理程序的data参数中。你看过那里包含的东西了吗?

#1


4  

The JSON response from the server should be in the data parameter in your success handler. Have you looked at what that contains?

来自服务器的JSON响应应该在成功处理程序的data参数中。你看过那里包含的东西了吗?