如何在asp.net MVC视图中解析ContentResult.Content类型的对象?

时间:2019-04-23 09:04:53

标签: javascript c# json asp.net-mvc parsing

我正在调用Web API,并且响应作为ContentResult.Content对象返回。我在asp.net MVC视图中检索了这些数据,并试图在标签中显示这些数据,但是我无法弄清楚如何将该对象解析为可以使用的对象。

控制器代码:

[HttpGet]
    public async Task<ActionResult> getCall()
    {
        string url = "http://localhost:51080/";
        string customerApi = "customer/1";

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync(customerApi);
            if (response.IsSuccessStatusCode)
            {
                string jsondata = await response.Content.ReadAsStringAsync();
                return Content(jsondata, "application/json");
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }
    }

查看:

@using MVCApp.Controllers;

@{
ViewBag.Title = "Dashboard";
if (Session["userID"] == null)
{
    Response.Redirect("~/Login/Index");
}
}

<div class="row">
<div class="col-md-4">
    <h2>Getting started</h2>
    <p>
    <!-- Labels with values here! -->
    </p>
</div>
<p id="rData">
</p>
<div class="col-md-4">
</div>
</div>

@section scripts {
<script>

$(document).ready(function () {
    $.getJSON('/Home/getCall/', function (data) {
      // parse object here
    });
});
</script>
}

0 个答案:

没有答案