-1

JQuery で戻りオブジェクトを読む 4 分前|LINK

GenericList オブジェクトを返す wcf サービスを作成しました。Jquery を使用してクライアント側でそのオブジェクトを読み取る必要がありますが、動作させることができます。

以下は私のコードです:

サービス方法:

[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"}]});
4

2 に答える 2

1

これは機能します。

$.ajax({
        type : "GET",
        dataType : "jsonp",
        url : 'http://localhost:55679/RESTService.svc/GetComments',
        success: function(data){
            console.log(data.Comment.length);
            for(var i=0; i<data.Comment.length; i++){
                console.log(data.Comment[i].Title);
            }
        }
    });
于 2013-03-19T17:17:08.463 に答える
1

これを試して:

$.getJSON('http://localhost:55679/RESTService.svc/GetComments?callback=?', function (data) {
    alert(data.Comments[0].Author);
});
于 2013-03-19T16:57:31.710 に答える