4 分钟前在 JQuery 中读取返回对象|LINK
我创建了一个返回 GenericList 对象的 wcf 服务。我需要使用 Jquery 在客户端读取该对象,但可以使其正常工作:
下面是我的代码:
svc 方法:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public CommentList GetComments()
{
Comments oComment1 = new Comments();
oComment1.Title = "AMaking hay when the sun shines";
oComment1.Author = "Plan_A";
oComment1.CommentText = "AI like hay almost as much as I like sun. Just joking";
Comments oComment2 = new Comments();
oComment2.Title = "Making hay when the sun shines";
oComment2.Author = "Plan_B";
oComment2.CommentText = "I like hay almost as much as I like sun. Just joking";
CommentList oCommentList = new CommentList();
oCommentList.Comment.Add(oComment1);
oCommentList.Comment.Add(oComment2);
return oCommentList;
}
客户端的 Jquery 脚本:
$('#CommentsButton').click(function () {
$.getJSON('http://localhost:55679/RESTService.svc/GetComments?callback=?', function (data) {
alert(data);
});
我得到的响应(通过 Inspect 元素 - Chrome 工具)
jsonp1363710839478({"Comment":[{"Author":"Plan_A","CommentText":"AI like hay almost as much as I like sun. Just joking","Title":"AMaking hay when the sun shines"},{"Author":"Plan_B","CommentText":"I like hay almost as much as I like sun. Just joking","Title":"Making hay when the sun shines"}]});