在回掉函数返回的时候 firefox 说返回的字符串对象未定义???
也就是这一句alert(json.msg);//显示反馈信息这是页面js 和html 代码
<script type="text/javascript">
<!--
  $(function() {
    $("#submit").click(function() {
  
       var username1 = $("input[name=username]").val();
       var password1 = $("input[name=password]").val();
       var JSONObject = {username:username1, password:password1};//JSON对象       var strUser = JSON.stringify(JSONObject); //将JSON对象转变成JSON格式的字符串
       //var strUser = JSONObject.toString();
       alert(strUser)
       $.post("login.action", {json: strUser}, callback(), "json");
});
  
function callback(json) {
    alert("123467");
    alert(json.msg);//显示反馈信息
    if (json.suc == 1) {
        window.location.href = "welcome.jsp";//跳转到后台主页
    }
}});
  -->
 </script><form action="login.action" method="post">
    账号<input type="text" name="username"/><br/>
    密码<input type="password" name="password"><br/>
    <input type="button" id="submit" value="登录"/>
</form>下面是action
public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L;  private String json;    //JSON字符串,JS与Action传递数据的载体

public void valid() {
System.out.println(json);
try {
JSONObject jsonObj = new JSONObject(json); //将JSON格式的字符串构造成JSON对象
String username = jsonObj.getString("username");//获取JSON对象中的loginName属性的值
String password = jsonObj.getString("password");//获取JSON对象中的password属性

 //此时的JSON对象,有两个属性suc和msg,其中suc表示是否登录成功的状态
if ("moonscope" == username || "moonscope".equals(username)) {
if ("123456" == password || "123456".equals(password)) {
json = "{suc:1, msg:'登陆成功!'}"; //构造JSON格式的字符串
}
json = "{suc:0, msg:'密码错误!'}";
}
json = "{'suc':0, 'msg':'用户不存在!'}";
System.out.println(json);
sendMsg(json);  //发送JSON格式的字符串回JS端
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

    /** 
     * 向客户端的JS发送字符串
     * @param content 发送的内容
     * @throws IOException
     */
    public void sendMsg(String content){
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        try {
response.getWriter().write(content);
} catch (IOException e) {
e.printStackTrace();
}
    } //getter  setter
public String getJson() {
return json;
} public void setJson(String json) {
this.json = json;
}
}

解决方案 »

  1.   

    function callback(json) {
      alert("123467");
      alert(json.msg);//显示反馈信息
      if (json.suc == 1) {
      window.location.href = "welcome.jsp";//跳转到后台主页
      }
    }改为function callback(json) {
      alert("123467");
      json= eval(json);//POST方法必加,ajax方法自动处理了  
      alert(json[0].msg);//显示反馈信息
      if (json[0].suc == 1) {
      window.location.href = "welcome.jsp";//跳转到后台主页
      }
    }