日期:2014-05-20  浏览次数:20652 次

求救。。struts2+jquery+json 总是返回error
js:
function forAjax()
{
  var code = document.getElementById("queryName").value;  
  $.ajax({
  url:"json!add",
  data:{'queryName':code},
  dataType:'json',
  type:'post',
  async: false,
  success:function(data){
alert(data);
  },
  error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("111"+textStatus);
}
  });
}

struts 的配置文件
<?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.i18n.encoding" value="UTF-8" />
<package name="crusol" extends="json-default">
<action name="json!*" class="com.gameco.awweb.dash.action.TestAction" method="{1}">
<result type="json"/>
</action>
</package>
</struts>

action
public class TestAction extends ActionSupport
{
private String queryName;
private String result; 
JSONObject jo = null;

public JSONObject getJo() {
return jo;
}

public void setJo(JSONObject jo) {
this.jo = jo;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}

public void add()
{
Map<String, String> map = new HashMap<String, String>(); 
CertificateType cer = new CertificateType();
cer.setId(0);
map.put("queryName", this.queryName);
jo = JSONObject.fromObject(cer);  
System.out.println(jo);
}

public String getQueryName() {
return queryName;
}

public void setQueryName(String queryName) {
this.queryName = queryName;
}
}

1.JS已经进入了action中:后台打印出{"name":"","id":0,"remove":0,"remark":""}
2.前台JS没有进SUCCESS,而是跑到error中打印出111error,就是运行了 error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("111"+textStatus);
}

我用的是struts2.1.8,对应的Jsonplugin2.1.8应该没有版本的问题!
请大家帮帮忙看看哪里出错了!谢谢

------解决方案--------------------
参数错了,{这里面不用加双引号}