框架struts1.2,spring2.5,hibernate3.2我在action里写一个login方法
[code=Jav]a
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
   StringBuffer sb = new StringBuffer("{");
   sb.append("url:'");
   sb.append(request.getContextPath());
   sb.append("/accind.do',");
   sb.append("state:'1'");
   sb.append("}");
   response.getWriter().write(sb.toString());
   return null;
}
[/code]
页面login.jsp中jquery调用ajax重要代码如下
[code=Jav]a
$.ajax({
       type: "POST",
url: "/login.do?method=login",
data:$("#login_box").serialize(),
success: function(data) {
if(data.state == 1) {
window.location.href = data.url;
} else if(data.state == -2) {
$("#l_sub").addClass("l_sub_hover").html('<u></u>登录');
error.show();
error_p.html("<i></i>"+data.msg+"");
yzm.show();

[/code]点提交按钮能够运行到action中的login方法,但却返回不到页面的ajax方法success中。
如果把success改成complete,可以得到返回对象,alert(data),显示的是[object],
alert(data.state),js报错
这到底是怎么回事,我该怎么解决,希望大家给我点意见。

解决方案 »

  1.   

    可以用firebug的网络标签页看一下返回了什么结果
      

  2.   

    更正一下,
    5.
    StringBuffer sb = new StringBuffer("{\"state\":\"1\"}");
    response.getWriter().write(sb.toString());
    success: function(data) {
      alert(data.state);
    }
    已可以正常执行success。alert(data.state)显示1.问题已解决:只有key和value都加双引号并且包含在中括号内才能正常执行。
    这里是java后台写法:如:{\"state\":\"1\"}
      

  3.   

    [{\"id\":\"1\",\"name\":\"name\"}];
    上面是java代码;
    或者
    sb.append("[{'id':'1','name':'name'}]");