gpt4 book ai didi

http - 在 Angular 的 $http 请求中到达 JSON 的正确子节点的点表示法不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:10:13 25 4
gpt4 key购买 nike

我是 AngularJS 的新手。我向服务器发布命令,服务器返回响应。我的 POST 命令工作正常,因为我将响应放在警报 View 中并将其作为 JSON 返回,因此我可以在警报 View 中看到响应。

这是我的代码:

var appControllers = angular.module('app', ['jsonService']);
appControllers.controller('post', function($scope, $http) {

$http({
withCredentials: true,
method : 'POST',
url : 'http://myURL/command',
data : 'myData',
headers : {
'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8'
}
}).success(function (data, status, headers, config) {
var JSONData = JSON.stringify(data);
$scope.persons = JSONData;
alert(JSONData);// assign $scope.persons here as promise is resolved here
}).error(function (data, status, headers, config) {
$scope.status = status;
alert(status);
});
});

我的问题:我想解析 JSON 中的一个特定项目。我知道我应该使用点符号来访问 JSON 的适当子级,但如果我使用点符号,它就不起作用了。

这是我的 HTML 代码:

<ul data-ng-controller="post">
<li ng-repeat="person in persons">
<div><a href="http://button2" data-role="button" ng-bind="person.firstName"></a></div>
</li>
</ul>

任何提示或帮助将不胜感激。

最佳答案

删除:

  var JSONData = JSON.stringify(data);

并且只使用“数据”。您正在将 javascript 对象转换回字符串。你想把它当作一个对象来使用。

  $scope.persons = data;

关于http - 在 Angular 的 $http 请求中到达 JSON 的正确子节点的点表示法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17796919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com