gpt4 book ai didi

javascript - angularJS $http.get 与 API 通信

转载 作者:行者123 更新时间:2023-12-02 16:20:52 24 4
gpt4 key购买 nike

我是 AngularJS 的新手,我正在尝试通过研究示例代码来理解它。

这里我有一个关于 $http.get 的内容,来自以下链接:

http://www.w3schools.com/angular/tryit.asp?filename=try_ng_customers_json

我刚刚用我自己的url替换了url,但它不起作用,我真的很困惑,请帮忙,谢谢!

==========================

第二次编辑:感谢答案,双ng-app是一个错误,但这不是这个问题的主要原因。我认为这与跨站点阻止有关,但我已在 API 上将其关闭(我使用 codeigniter REST API 并设置 $config['csrf_protection'] = FALSE; ),我不确定如何配置它。

<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="customersCtrl">
<ul>
{{names}}
</ul>
</div>
<div ng-controller="myCtrl">
<ul>
{{names}}
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.w3schools.com/website/Customers_JSON.php")
.success(function (response) {$scope.names = response;});
});
app.controller('myCtrl', function($scope, $http) {
$http.get("https://manage.pineconetassel.com/index.php/api/v1/colors2.php")
.success(function (response) {$scope.names = response;});
});
</script>
</body>
</html>

最佳答案

问题是您有两个 "myApp" 声明。来自 AngularJS documentation :

Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application.

因此,您应该将 ng-app="myApp" 移动到 body 元素。但是,一旦完成,您可能仍然看不到任何结果,因为您正在跨站点脚本编写,并且浏览器将(默认情况下)阻止第二个请求。

关于javascript - angularJS $http.get 与 API 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29137521/

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