我正在开发一个jquery插件,它从php页面获取json数据。现在我想在asp.net Web服务中做同样的事情。但我发现很难理解PHP代码。由于限制,我也无法在iis中托管php页面。请帮我修改asp.net webservice的代码。
<?php
$year = date('Y');
$month = date('m');
echo json_encode(array(
array(
'id' => 111,
'title' => "Event1",
'start' => "$year-$month-10",
'url' => "http://yahoo.com/"
),
array(
'id' => 222,
'title' => "Event2",
'start' => "$year-$month-20",
'end' => "$year-$month-22",
'url' => "http://yahoo.com/"
)
));
?>
用于阅读的Jquery代码......
$(document).ready(function () {
$('#calendar').fullCalendar({
eventSources: [
// your event source
{
url: '/myfeed.php',
type: 'POST',
data: {
custom_param1: 'something',
custom_param2: 'somethingelse'
},
error: function () {
alert('there was an error while fetching events!');
},
color: 'yellow', // a non-ajax option
textColor: 'black' // a non-ajax option
}
]
});
});
答案 0 :(得分:2)
这是将输出的json代码
[
{
"id": 111,
"title": "Event1",
"start": "<current year>-<current month>-10",
"url": "http:\/\/yahoo.com\/"
},
{
"id": 222,
"title": "Event2",
"start": "<current year>-<current month>-20",
"end": "<current year>-<current month>-22",
"url": "http:\/\/yahoo.com\/"
}
]
由于这个写asp代码
答案 1 :(得分:2)
阅读http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/和http://encosia.com/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/
正确的web服务应该输出JSON对象。
如果您需要JSON序列化程序,请查看:http://json.codeplex.com/
使用@ safarov的代码,您应该能够调用serialise方法将其作为JSON字符串返回:
Newtonsoft.Json.JsonConvert.SerializeObject(someObject)