JQuery AJAX call - how to capture a JSON response?

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> the 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 to access the JSON returned by the Action class? I use struts2-json-plugin, which automatically converts the Action class to JSON. I want to be able to update a div tag based on a JSON response.

This is what I see on the console:

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

But I'm not sure how to write this to the JSP. Please help. Gh

+3
source share
1 answer

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

+4
source

Source: https://habr.com/ru/post/1772779/


All Articles