当前位置:编程学习 > JS >>

$.ajax 返回JSON 数据及无法成功解决办法

注:得改成这样写:"{"success":true,"mesg":"success"}",如果说是bool类型,则不用加引号,其它的键/值都需要加引号.

$.ajax({
..
dataType:'json',
...
success:function(json){
//不执行success
},
error:function(error){
//总是执行这个error
}
});


经常用的是$get(url,data,callback,type)方法

其中url是异步请求的页面(可以是.ashx文件),data是参数,callback是回调函数,而type是返回数据的类型.type有xml,html,json,text等.

首先,页面引用jquery.js

在页面写ajax处理的js函数

function initMeeting() {
            $.get("/Common/MeetingRoom.ashx", {meetid:<%=meetId %>},function sellerList(data){
                $("#divSellerList").html(data);
            },"json");
            setTimeout("initMeeting()",20000);
        }
        function initMeeting() {
            $.get("/Common/MeetingRoom.ashx", {meetid:<%=meetId %>},function sellerList(data){
                var obj = eval( "(" + data + ")" );//转换后的JSON对象
                $("#divSellerList").html(obj.CellerList);
            },"html");
            setTimeout("initMeeting()",20000);
        }

我用的返回类型是json,这样可以返回类似类的数据类型.比如{"Name":"Sunny D.D", "Age":25}

但是在使用返回值data时,首先要转换json,通过

var obj = eval( "(" + data + ")" );//转换后的JSON对象

就能获得json对象.

 

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,