我在angularjs中创建了一个登录页面,经过身份验证后我从web-api获取了json中的数据,但当我以这种方式尝试时,这个javascript代码无效。
这是demoApi.js文件,该文件带来数据,但给出错误-1
(function () {
var DemoApi = function ($http) {
var loginCheck = function () {
alert("login...");
return $http.get('http://localhost:35456/api/customer/')
.success(function (data, status, headers, config) {
$scope.posts = data;
alert("recived data");
//alert(data.data.ID);
})
.error(function (data, status, headers, config) {
// log error
alert("error"+status);
});
};
return {
loginCheck: loginCheck
}
};
var app = angular.module("angularApp");
app.factory("DemoApi", DemoApi);
}());
但如果我在html页面中编写此脚本,我将从web-api
获取数据<script>
var app = angular.module("MyApp", []);
app.controller("PostsCtrl", function ($scope, $http) {
$http.get('http://localhost:35456/api/customer/Dipti123/dipti').
success(function (data, status, headers, config) {
$scope.posts = data;
alert("recived data");
//alert(data.data.ID);
}).
error(function (data, status, headers, config) {
// log error
alert("error");
});
});
</script>
这是我的应用的索引页
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="angularApp">
<head>
<title>Ebank</title>
<script src="Scripts/angular.js"></script>
<script src="Scripts/angular-route.js"></script>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<script src="lib/app.js"></script>
<script src="lib/bankApi.js"></script>
<script src="lib/homeController.js"></script>
<script src="lib/loginController.js"></script>
<script src="lib/infoController.js"></script>
</head>
<body style="background-color:#333">
<div ng-view></div>
</body>
</html>
任何人都可以告诉我Demoapi.js文件的问题是什么