我有一个usercontrol
项目。我VisualWebPart
想要一个 ajax 调用来获取 json 数据并在 Jquery 插件中使用它。简单来说,web applications
我使用了一个返回 JSON 数据的 Web 服务,并通过 Ajax 在我的页面中调用它,这听起来像是我无法使用 Web 服务,甚至Web Methods
在我的 .ascx 控件中,所以我如何调用我的方法UserControl
来获取 JSON 数据。
编辑:我有这段代码,我也想在 a 中执行此操作UserControl
。在WebService
:
public class getTimeLineService : System.Web.Services.WebService
{
[WebMethod]
public String getJsonTimeLine()
{
List<TimeLine> list = new List<TimeLine> { new TimeLine { headline = "Vine", text = "<p>Vine Test</p>", startDate = "1391,12,12", endDate = "1392,1,27" }, new TimeLine { headline = "Sh*t Politicians Say", text = "<p>In true political fashion, his character rattles off common jargon heard from people running for office.</p>", startDate = "1392,1,26", endDate = "1392,1,27" } };
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
StringBuilder sb = new StringBuilder();
serializer.Serialize(list, sb);
return sb.ToString();
}
}
并WebService
通过 ajax 使用它:
$.ajax({
type: "POST",
url: "getTimeLineService.asmx/getJsonTimeLine",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var jsonDate = eval( data.d );
},
failure: function (msg) {
alert(msg);
}
});