我在使用Angular调用外部JSON文件时遇到了问题,事实上,本地调用完全正常,但是当我使用完整的URL进行调用时,会返回404错误。以下是我的代码,如果您发现任何问题,请告诉我,感谢:
// JavaScript Document
var angularTodo = angular.module('lostsysApp', []);
function mainController($scope, $http) {
$scope.names = [];
$http.get('http://www.viudadesoubrier.com/angular/model.php')
.success(function(data) {
$scope.names = eval(data);
console.log(data)
})
.error(function(data) {
alert(data);
console.log('Error: ' + data);
});
$scope.addNom = function() {
$http.post('http://www.viudadesoubrier.com/angular/model.php', { op: 'append', nom: $scope.nom, telefon: $scope.telefon } )
.success(function(data) {
$scope.names = eval(data);
console.log(data)
})
.error(function(data) {
console.log('Error: ' + data);
});
$scope.nom="";
$scope.telefon="";
}
$scope.delNom = function( nom ) {
if ( confirm("Seguro?") ) {
$http.post('http://www.viudadesoubrier.com/angular/model.php', { op: 'delete', nom: nom } )
.success(function(data) {
$scope.names = eval(data);
console.log(data)
})
.error(function(data) {
console.log('Error: ' + data);
});
}
}
}
添加index.html的代码。
<!doctype html>
<html ng-app="lostsysApp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="mainController">
<div class="jumbotron text-center">
<h1>Angular Test</h1>
</div>
<div class="col-sm-8 col-sm-offset-2 text-center">
<div class="form-group">
<input type="text" ng-model="nom" placeholder="Contact Name" class="form-control input-lg text-center" />
</div>
<div class="form-group">
<input type="text" ng-model="telefon" placeholder="Phone Number" class="form-control input-lg text-center" />
</div>
<div class="form-group">
<button class="btn btn-primary btn-lg" ng-click="addNom()">Añadir</button>
</div>
<div ng-repeat="n in names">
<p>
{{n.nom}} ({{n.phone}})
<a href="#" ng-click="delNom(n.nom)">[X]</a>
</p>
</div>
</div>
</body>
</html>
谢谢你。
感谢你。