gpt4 book ai didi

angularjs - 无法在 Angular 中使用 $http 发送 POST 请求 - ReferenceError : $http is not defined

转载 作者:行者123 更新时间:2023-12-03 08:16:08 27 4
gpt4 key购买 nike

所以我一直收到这个 ReferenceError: $http is not defined,即使我在 Controller 中包含了 $http,这似乎是最常见的原因这个错误信息。我也试过将 $http 传递给函数本身,但这并没有解决问题。

我觉得我遗漏了一些非常明显的东西,所以非常感谢任何帮助,谢谢!

为了清楚起见,我已经包含了整个脚本。您可以在脚本末尾的 finaliseDay 函数中看到发布请求。

谢谢!

这里是错误:

ReferenceError: $http is not defined
at l.$scope.finaliseDay (http://localhost:8888/goalzy.js:69:12)
at ib.functionCall (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:198:303)
at Dc.(anonymous function).compile.d.on.f (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:214:485)
at l.$get.l.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:125:305)
at l.$get.l.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:126:6)
at HTMLAnchorElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:215:36)
at HTMLAnchorElement.c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:32:389)angular.js:11607 (anonymous function)angular.js:8557 $getangular.js:14502 $get.l.$applyangular.js:21440 (anonymous function)angular.js:3014 c

首先是HTML

<!doctype html>
<html ng-app="goalzy">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script>
<script src="goalzy.js"></script>
</head>
<body>
<div class="container">
<div class="well">
<h2>Goalzy</h2>

Dev TODO
<ul>
<li>Hook up the API to persist data</li>
</ul>

<div ng-controller="TodoController">
<span>{{remaining()}} of {{todos.length}} remaining today</span>
<span>You're at {{percentComplete()}}% completion</span>
[ <a href="" ng-click="finaliseDay(percentComplete())">finalise day</a> ]
<ul class="unstyled">
<li ng-repeat="todo in todos">
<input type="checkbox" ng-model="todo.done">
<span class="done-{{todo.done}}">{{todo.text}}</span>
</li>
</ul>
<form ng-submit="addTodo()">
<input type="text" ng-model="todoText" size="30"
placeholder="add new todo here">
<input class="btn-primary" type="submit" value="add">
</form>
<hr>
<div class="historial" ng-repeat="h in historicalDailyPercentages">
<ul>
<li>Date: {{h.date}}</li>
<li>Percentage of Daily Tasks Completed: {{h.percent}}%</li>
<li><div>Tweet it!</div></li>
</ul>

</div>
</div>
</div>
</div>
</div>
</body>


</html>

这是 JS:

//Goalzy.js

angular.module('goalzy', [])

.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
});

.controller('TodoController', ['$scope','$http', function($scope, $http) {
$scope.todos = [];

$scope.historicalDailyPercentages = [];

$scope.addTodo = function() {

if ($scope.todoText != "") {
if ($scope.todos.length < 3) {
$scope.todos.push({text:$scope.todoText, done:false});
$scope.todoText = '';

//Save to DB

}
else {
alert("You can only have 3 todos per day!");
$scope.todoText = '';
}
} else {
alert("you must write something");
}

};

$scope.remaining = function() {
var count = 0;
angular.forEach($scope.todos, function(todo) {
count += todo.done ? 0 : 1;
});
return count;
};

$scope.percentComplete = function() {
var countCompleted = 0;
angular.forEach($scope.todos, function(todo) {
countCompleted += todo.done ? 1 : 0; //Simply calculates how many tasks have been completed
console.log(countCompleted);
});

var totalCount = $scope.todos.length;
var percentComplete = countCompleted / totalCount * 100;
return percentComplete;
}


$scope.finaliseDay = function(percentComplete) {
alert("You're finalising this day with a percentage of: " + percentComplete);
var today = new Date();
var alreadyPresent = $scope.historicalDailyPercentages.some(function (item) {
return item.date.getFullYear() === today.getFullYear() &&
item.date.getMonth() === today.getMonth() &&
item.date.getDate() === today.getDate();
});

//Confirm that nothing has alreayd been posted for today
if (!alreadyPresent) {
$scope.historicalDailyPercentages.push({
percent: percentComplete,
date: today
});

// Simple POST request example (passing data) :
$http.post('/api/postDailyPercentage.php', {msg:'hello word!'}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
console.log("data" + data);
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("data" + data);
});

}
else {
alert("You're all set for today - see you tomorrow!");
}

console.log($scope.historicalDailyPercentages);
}


}]);

最佳答案

Provider 在后缀为 'Provider' 的 Controller 中不可用,你只能通过提供者名称访问它们,这里只能是 $http,同时删除 ;配置初始化后

$httpProvider 设置应该在 Angular 配置阶段完成

代码

angular.module('goalzy', [])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
}]);
.controller('TodoController', ['$scope', '$http', function($scope, $http) {
//controller code here
}]);

注意:您肯定应该删除 $httpProvider.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8'; 来自 Controller 的行

Working Plunkr

关于angularjs - 无法在 Angular 中使用 $http 发送 POST 请求 - ReferenceError : $http is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28914152/

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