Javascript 如何在AJAX中处理JSON输出?
我在.js文件中有这个代码片段Javascript 如何在AJAX中处理JSON输出?,javascript,jquery,ajax,json,asp.net-mvc-3,Javascript,Jquery,Ajax,Json,Asp.net Mvc 3,我在.js文件中有这个代码片段 $(function () { // KeyDates var url = "http://localhost:8732/Design_Time_Addresses/Intel.IIP.MDF.WCF/ProgramCalendarService/GetKeyDatesCalendarNew"; $.ajax({ url: url, data: null, type: 'POST',
$(function () {
// KeyDates
var url = "http://localhost:8732/Design_Time_Addresses/Intel.IIP.MDF.WCF/ProgramCalendarService/GetKeyDatesCalendarNew";
$.ajax({
url: url,
data: null,
type: 'POST',
contentType: 'application/json',
dataType: 'json',
success: function (GetKeyDatesCalendarDataNew) {
alert(GetKeyDatesCalendarDataNew);
$(document).ajaxStop($.unblockUI);
}
});
});
如何处理GetKeyDateScalendardAtanNew中的键值对?您可能想知道如何访问对象的道具。为此,使用
For in
循环迭代对象的值:
success: function (GetKeyDatesCalendarDataNew) {
for(var key in GetKeyDatesCalendarDataNew)
{
var value = GetKeyDatesCalendarDataNew[key];
// do somehitng based on the key and/or value iterated
}
}
对于这种情况,success函数的参数是从Ajax请求返回的经过计算的JSON。因此,
getkeydatescalendardatnew
,您应该将其重命名为类似于data
,成为服务器返回的实际数据
只有了解数据的结构,才能处理数据。了解这一点的一个简单方法是执行console.log(GetKeyDateScalendardAtanNew)
,然后使用for
循环(如果是数组)轻松处理它,或者for x in.
如果是对象。您可以使用JQuery“getJSON”函数,您需要在其中传递url并指定回调函数。您的ajax调用将由getJSON函数处理。在回调函数中,可以作为属性访问键。漂亮的
请向我们展示您的json响应示例,以帮助您。另一个不相关的方面是,您只需编写
$。Unbui
您几乎处于ajaxStop阶段。您能否向我们展示GetKeyDatesCalendarDataNew
的数据结构?[{“EventDate”:“\/Date(1297189800000+0530)\/”,“EventText”:“Lorem ipsum dolor sit amet,Concertetur Adivising Elite.”,“EventType”:3,“Logos”:null}{“EventDate”:“\/Date(1299263400000+0530)\/”,“EventText”:“Lorem ipsum door sit amet,concertetur adipising elit.”,“EventType”:3,“Logos”:null}当我看到WCF方法的输出时,是这样的。
$.each(GetKeyDatesCalendarDataNew,function(key,value){
//do something here
})