1
angular.module('myApp', ['ngRoute', 'myApp.controllers'])
.config(function ($routeProvider, $httpProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/views/myApp.html',
controller: 'appController'
})
.otherwise({
redirectTo: '/'
});
});
angular.module('myApp.controllers', [])
.controller('appController', function($scope, $http) {
$scope.oneCharacter = function() {
$http({
method:'GET',
url:'http://localhost:3000/#/api/info'
}).then(function(response) {
console.log('-------------------------', response);
}) .catch(function(err) {
console.log(err);
});
}
});
<div ng-controller="appController">
<input type="button" value="button" ng-click="oneCharacter()"/>
</div>
<!DOCTYPE html>
<html ng-app='myApp'>
<head>
<meta charset="utf-8" />
<title>Hello</title>
</head>
<body>
<div ng-view></div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.js"></script>
<script src="app/controllers/myApp.js"></script>
<script src="app/app.js"></script>
</body>
</html>
こんにちは、 私が構築http://localhost:3000/#/api/info
サーバにGETリクエストを送信しています。私はjson
オブジェクトが人々の情報でいっぱいになることを期待しています。ただし、appController
(コントローラ)のbutton
をクリックするたびに、リクエストは指定されたurl
にはなりません。私が後でindex.html
の内容を取得するたびに、私は以下のようにコピーしました。しかし、私はpostman
を使用してサーバーをテストし、正しい情報を返します。誰もがなぜそれが起こっているか知っていますか?角度は、内部ルーティングを行う行うために使用するものである:(アンカー別名) おかげ
'http:// localhost:3000 /#/ api/info'は 'http:// localhost:3000/index.html#/ api/info'と同じです。あなたのフロントエンドとは違うポートに。 –