public ActionForward news(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

String id = request.getParameter("id");
News news = (News)companyService.getObject(News.class, id);

response.setContentType("text/html;charset=utf-8");  
StringBuffer sb = new StringBuffer();
sb.append("{");
sb.append("title:'").append(news.getTitle()).append("',");
sb.append("subTitle:'").append(news.getSubTitle()).append("',");
sb.append("createDate:'").append(news.getCreateDate()).append("',");
sb.append("content:'").append(news.getContent()).append("',");
sb.append("picturePath:'").append(news.getPicturePath()).append("'");
sb.append("}");

System.out.println(sb);
response.getWriter().print(sb);
response.getWriter().flush();

return null;

}$.ajax({
 type:"POST",
 url:"login.do?method=news&id="+id,
 dataType:"json",
 success: function(data){
$.each(data , function(key , xx){
alert(data[key]);}
)},
error:function(){
alert("11111111111");
}
  });为什么 客户端请求之后 就直接到error  这里啦?
还有 服务器端 自己封装json 是这样吗?  为什么注释掉 “ dataType:"json",
” 这个 ,服务器 返回的 好像是文本类型.  
请知道的提出正确的方式.

解决方案 »

  1.   

    服务器断  生成的 是 这样的格式:
    {title:'sf',subTitle:'s',createDate:'2010-04-14 11:38:39.0',content:'<p>sdf</p>',picturePath:''}难道 这个不行吗?
      

  2.   

    楼主你引用了jquery的库了吗?这个函数是jquery封装的ajax的
      

  3.   

    返回的就是文本类型的。用eval执行下就可以了
      

  4.   

    {“name”:“name”}都这样弄上就可以了
    加双引号啦
    java里双引号这样/"使用/反义字符
      

  5.   

    六楼正解。一般后台的JSON字符串自己拼写会比较麻烦,也比较容易出错,可以使用JSONObject          import net.sf.json.JSONObject;   JSONObject jsonObject = JSONObject.fromObject(news);
      System.out.println(jsonObject.toString());
      

  6.   

    使用
    $.getJSON("login.do?method=news&id="+id,function(data){
       alert(data);  //检查一下返回的数据
    });
      

  7.   

    如果你引用的JQUERY版本是1.4.2的话,你后台生成的JSON语句一定要标准必须是
    {"Name":"Joe","Age":"18",……}键和值都要双引号。
    1.3.2就不需要。
      

  8.   

    首先应该看看到error里的原因,看看是什么错误,LZ可以alert出来返回的内容,或者用FireBug调试一下。
      

  9.   

    对,用FireBug调试一下。找到问题所在。
      

  10.   

    肯定是你json的格式错误。仔细检查一下吧
      

  11.   

    多谢大家啦 已经解决啦
    sb.append("content:'").append(news.getContent()).append("',");
    是这个里面有回车 和 空格