JQuery的AJAX调用 - 如何捕捉响应JSON? [英] JQuery AJAX calls - how to capture response JSON?

查看:109
本文介绍了JQuery的AJAX调用 - 如何捕捉响应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;
 });
});

<形式GT; 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类:

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;  
 }  

问题:我如何访问JSON返回的Action类?我使用Struts2的JSON的-插件,它可以自动Action类转换成JSON。我希望能够更新基于JSON响应div标签。

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":"HELLO","username":"ABCD"}

但我不知道如何抓住这个在JSP。请帮忙。 GH

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

推荐答案

来自服务器的JSON响应应该在数据参数,在成功处理程序。你看什么,它包含?

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

这篇关于JQuery的AJAX调用 - 如何捕捉响应JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆