简体   繁体   English

如何在 HTML 中显示 HTTPresponse 对象的内容?

[英]How do I display content from a HTTPresponse object in HTML?

I have a MEAN stack application in which I am accessing the node backend as an API.我有一个 MEAN 堆栈应用程序,在其中我将节点后端作为 API 访问。 I am able to set the response message and am also able to see it in the response object in my browser.我可以设置响应消息,也可以在浏览器的响应对象中看到它。 How will I be able to access this message and display it in my HTML?我如何才能访问此消息并将其显示在我的 HTML 中?

Screenshot of Network tab in browser showing the response object:浏览器中显示响应对象的网络选项卡的屏幕截图:

[ [ 显示响应对象的浏览器中网络选项卡的屏幕截图]

This is a very broad question.这是一个非常广泛的问题。 Really, what you've said is - "I've got the server-side, how do I do the client-side."真的,你所说的是 - “我有服务器端,我如何做客户端。”
Anyway, you can use Angular.无论如何,您可以使用 Angular。 With Angular, you can do this:使用 Angular,你可以这样做:

$http.get('/items').then(function(response) {
    $scope.data = response.data;
});

That gets the data to Angular(mind you, this is simplified), which can then show it in the html through templates like this:这将数据传送到 Angular(请注意,这是简化的),然后可以通过这样的模板在 html 中显示它:

<div ng-repeat="item in data">{{ item.name }}</div>

In your example, you're actually showing a null result.在您的示例中,您实际上显示的是空结果。 I would actually be managing the messaging for this in the Angular template something like this:我实际上会在 Angular 模板中为此管理消息传递,如下所示:

<div ng-if="!data">No items</div>

Actually, you're showing a result that appears to be an unsuccessful user lookup.实际上,您显示的结果似乎是不成功的用户查找。 For that, you'd be creating a script like this(again, this is simplified):为此,您将创建一个这样的脚本(同样,这是简化的):

$http.get('/users/' + id).then(function(response) {
    $scope.user = response.data.user;
});

and a template like this:和这样的模板:

<div ng-if="user">
    <h1>{{ user.name }}</h1>
    <h3>Favorite Books<h3>
    <div ng-repeat="book in user.books">{{ book }}</div>
</div>
<div ng-if="!user">Couldn't find user</div>

Hope this helps.希望这可以帮助。

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

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