繁体   English   中英

解析ajax json响应

[英]Parse ajax json response

我无法获取JSON对象的数据。

这是我的API动作:

    public string GetVideoInfo(uint videoID)
    {
        ApiVideoInfo videoInfo = new ApiVideoInfo()
        {
            Likes = BitVidDb.GetLikes(videoID),
            Dislikes = BitVidDb.GetDislikes(videoID),
            Views = BitVidDb.GetViews(videoID),
        };

        return JsonConvert.SerializeObject(videoInfo);
    }

如果我在浏览器中调用API,它将返回:

“{\\” 视图\\ “:396,\\” 喜欢\\ “:1,\\” 不喜欢\\ “:0}”

但是,当我调用此ajax函数时:

    $.ajax({
        url: '/API/Video/GetVideoInfo/25',
        dataType: 'application/json',
        complete: function (data) {
            var json = JSON.parse(data);
            alert(json["Views"]);
        },
    });

它给了我以下错误:

SyntaxError:JSON.parse:JSON数据的第1行第2列出现意外字符

我使用JSON.stringify将其转换为字符串,并输出以下内容:

{ “readyState的”:4 “responseText的”: “\\”{\\\\ “查看\\\\”:396,\\\\ “喜欢\\\\”:1,\\\\ “不喜欢\\\\”:0} \\ “”,”状态 “:200,” 状态文本 “:” OK“}

我需要采取一些其他步骤来获取这些值吗? 该请求似乎很好,Chrome开发人员工具将此作为api的答案:

JSON {“查看”:396,“喜欢”:1,“不喜欢”:0}

提前致谢

一月

您只能解析字符串,并且数据是一个对象。 要获取json作为字符串,我只需要使用data.responseText即可。

$.ajax({
    url: '/API/Video/GetVideoInfo/25',
    dataType: 'application/json',
    complete: function (data) {
        var json = JSON.parse(data.responseText);
        alert(json["Views"]);
    },
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM