baidu_15238971 2015-04-22 14:34 采纳率: 50%
浏览 3346
已采纳

ajax获取Servlet中的JSON的问题。

在js中请求ajax,把jsp中的一个Cno传到Servlet中

 $.ajax({
                type :"post",
                url : "ShowDetail?Cno="+Cno,
                async:false, 
                dataType : "json",              
                success : function(data) {              
                },
                error : function() {
                    var student =eval("(" +result +")");
                    alert(student.Cth1);
                } 
            });     

在Servlet中获取后台值,并out.print JSON格式

 out.print("[{'Cno':'"+Cno+"','Cth1':'"+cd.get(0).getCth1()+"']}); 

应该如何通过ajax获取这里面的值

我通过 var student =eval("(" +result +")"); 的方法,但是student.Cth1获取不了值

这里用Error是因为Success没有反应。

  • 写回答

5条回答

  • Go 旅城通票 2015-04-23 01:18
    关注

    你的jquery版本>1.4,json格式不标准是不会执行回调的,因为jquery1.4+指定dataType为json,标准json格式字符串才会执行回调

     out.print("[{\"Cno\":\""+Cno+"\",\"Cth1\":\""+cd.get(0).getCth1()+"\"]}); 
    
    
     $.ajax({
                    type :"post",
                    url : "ShowDetail?Cno="+Cno,
                    async:false, 
                    dataType : "json",              
                    success : function(data) {     
                                    alert(data[0].Cth1);///你返回的是json数组数组,不是json对象
                    },
                    error : function() {
                       // var student =eval("(" +result +")");
                       // alert(student.Cth1);
                    } 
                });  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题