//Create an Angular Module.
var newsModule = angular.module('NewsModule', []);
//Create an Angular Controller.
newsModule.controller('newsCtrl', ['$scope', '$http', function ($scope, $http) {
//function retrives POST,UPDATE,DELETE,GET data
$http.defaults.headers.put = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'
};
$http.defaults.useXDomain = true;
$scope.throughdata = function (){
delete $http.defaults.headers.common['X-Requested-With'];
$http.get('http://www.google.com').then(function(data,error){
alert(data);
alert(error);
$scope.days=data.data;
});
}
}
]);
Run Code Online (Sandbox Code Playgroud)
但我有以下错误
XMLHttpRequest无法加载http://www.google.com/.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许原点'null'访问.
当用户输入正确的用户名和密码时,我想重定向到另一个位置.但是当我$location.path('dashboard')
在这里使用时,浏览器的URL被更改但是该页面没有被加载.当使用ctrl + R刷新我的页面或者点击浏览器的刷新图标然后加载适当的页面.
$http.post('/login', $scope.userInfo)
.success(function (data) {
//Check if the authentication was a success or a failure
//Successful POST here does not indicate success of authentication
if (data.status === "authentication success") {
//Proceed to load the dashboard for the user
$location.path('/dashboard');
} else if (data.status === "authentication error") {
//Inform user of the error
$scope.errorMessage = data.error;
$scope.showErrorMessage = true;
}
})
.error(function (error) {
$scope.errorMessage = "Error while attempting to authenticate. Check log.";
$scope.showErrorMessage = true; …
Run Code Online (Sandbox Code Playgroud) 我用这种方式上传文件:
<input type="file"
name="upload-file"
ng-model= "excelFile"
accept=".xlsx"
onchange="angular.element(this).scope().fileChanged(this);"
required="true"
/>
Run Code Online (Sandbox Code Playgroud)
在控制器中创建fileChanged方法
$scope.fileChanged = function(files) {
$scope.excelFile = files[0];
};
Run Code Online (Sandbox Code Playgroud)
它适用于FireFox,Chrome IE10,IE11,但在IE9中它显示"文件为null我们未定义".