不知道是哪里写错了,总是不能正常的接受到数据package test;import java.util.ArrayList;
import java.util.List;import net.sf.json.JSONArray;import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport{

private String josnStr;
public String test(){
List<String> list=new ArrayList<String>();
list.add("1");
list.add("1");
JSONArray json=JSONArray.fromObject(list);
josnStr=json.toString();
System.out.println("执行"+josnStr);
return this.SUCCESS;

}


//----------------------------------------------

public String getJosnStr() {
return josnStr;
}
public void setJosnStr(String josnStr) {
this.josnStr = josnStr;
}
}
这个是 action代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />   <package name="json" namespace="/" extends="json-default">
<action name="test" class="test.TestAction" method="test">
<result type="json"/>
</action>
</package>
</struts>
配置文件,返回 JSON格式,下面JSP页面获取。
<%@ page pageEncoding="GBK" import="java.util.*"%>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">
function test()
{
 
$.ajax(
url:'test.action',
type:'post',
dataType:'json',
success:function(data){
alert(data);
}
error:function(){
alert('errot');
}
);
}
</script>
</head>

<body>
<a href="javascript:;" onclick="test();">测试</a>
</body>
</html>不知道是哪里写错了,请各位大侠帮忙解决下,这个地方不明白,我的项目没办法继续下去了。

解决方案 »

  1.   


    public class TestAction extends ActionSupport{private List<String> list;
    public String test(){
     list=new ArrayList<String>();
    list.add("1");
    list.add("1");
    return this.SUCCESS;}
    list set and get 方法这样试试你既然用了struts2的json插件。。那么就不需要自己去手动把list转成json对象、、交给struts2就行
      

  2.   

    我是用out.print(json)把json输出出去的,然后return null,而不是return "success"; 也没有type="json"。
    然后我用$.getJSON("xx.action",function(data)
    {});来获取json
      

  3.   

    要自己想办法解决撒  首先 你把System.out.println("执行"+josnStr); 返回的json字符串已经输出到了控制台了 然后 你在js 中 获取 回调函数的data 参数信息 你要对着你的输出得json数据 alert 就是写data.x.x这样就可以了 建议用 firefox浏览器和firebug 你一看返回的数据格式 就清楚了