I am trying multiple ways to access my users in a local json file in able to later compare them to the users input. and if there is a match, access is allowed, but my main problem now is just getting to those users. My code so far:
我正在尝试多种方法来访问本地json文件中的用户,以便稍后将其与用户输入进行比较。如果有匹配,访问是允许的,但我现在的主要问题是如何访问这些用户。到目前为止我的代码:
完整的代码
json文件
What am i doing wrong? i am such a newbie in programming. I have been trying different things and nothing works. Thanks so much for help
我做错了什么?我是编程方面的新手。我一直在尝试不同的东西,什么也不做。非常感谢你的帮助
3 个解决方案
#1
2
Can you access the file through the browser (via your url localhost:8080/resources/data/users.json)?
可以通过浏览器(通过url localhost:8080/resources/data/user .json)访问文件吗?
If you can't, you will not be able to get access through the $resource or $http.
如果不能,您将无法通过$resource或$http访问。
If you can, any method should work:
如果可以,任何方法都应该有效:
1) Via $resource
1)通过美元的资源
$scope.users = [];
var UsersResource = $resource('/resources/data/users.json');
where we can get response by callback
哪里可以通过回调得到响应
UsersResource.get({}, function(response) {
$scope.users = response;
});
or by $promise
美元或承诺
UsersResource.get().$promise.then(function(response) {
$scope.users = response;
});
2) Via $http.get
2)通过http.get美元
$scope.users = [];
$http.get('/resources/data/users.json').then(function(response) {
$scope.users = response;
});
In your sample, your are trying to get array of users by returning $resource, but $resource returns a object with methods. Each method has callbacks (success, error) or return $promise object.
在示例中,您试图通过返回$resource来获取用户数组,但是$resource返回一个带有方法的对象。每个方法都有回调(成功、错误)或返回$promise对象。
#2
0
There is no need to use $resource
if you are just going to fetch a json file. Use $http
instead:
如果只是获取json文件,则不需要使用$resource。使用美元http:
this.getUsers = function(){
return $http.get('path/to/file.json');
};
And usage:
和用法:
dataService.getUsers().then(function(resp){
//Do something with the data
$scope.users = resp;
})
$resource
is meant to be used when communicating with RESTful apis. What your getUsers()
is doing is actually returning a resource-object, upon which you can then call get()
. But I recommend using $http
in this case.
$resource应该在与RESTful api通信时使用。getUsers()所做的实际上是返回一个resource对象,然后可以在这个对象上调用get()。但是我建议在这种情况下使用$http。
#3
0
If you want to use $resouce then you need to create two functions in your controller/factory where "templatesSuccess" return data of request.
如果您想使用$resouce,那么需要在控制器/工厂中创建两个函数,其中“templatesSuccess”返回请求数据。
getAllTemplates: function(query) {
return $resource(CONST.CONFIG.BASE_URL + 'receiver/get-templates').get(query, obj.templatesSuccess).$promise;
},
templatesSuccess: function(response) {
obj.allTemplates = response;
},
#1
2
Can you access the file through the browser (via your url localhost:8080/resources/data/users.json)?
可以通过浏览器(通过url localhost:8080/resources/data/user .json)访问文件吗?
If you can't, you will not be able to get access through the $resource or $http.
如果不能,您将无法通过$resource或$http访问。
If you can, any method should work:
如果可以,任何方法都应该有效:
1) Via $resource
1)通过美元的资源
$scope.users = [];
var UsersResource = $resource('/resources/data/users.json');
where we can get response by callback
哪里可以通过回调得到响应
UsersResource.get({}, function(response) {
$scope.users = response;
});
or by $promise
美元或承诺
UsersResource.get().$promise.then(function(response) {
$scope.users = response;
});
2) Via $http.get
2)通过http.get美元
$scope.users = [];
$http.get('/resources/data/users.json').then(function(response) {
$scope.users = response;
});
In your sample, your are trying to get array of users by returning $resource, but $resource returns a object with methods. Each method has callbacks (success, error) or return $promise object.
在示例中,您试图通过返回$resource来获取用户数组,但是$resource返回一个带有方法的对象。每个方法都有回调(成功、错误)或返回$promise对象。
#2
0
There is no need to use $resource
if you are just going to fetch a json file. Use $http
instead:
如果只是获取json文件,则不需要使用$resource。使用美元http:
this.getUsers = function(){
return $http.get('path/to/file.json');
};
And usage:
和用法:
dataService.getUsers().then(function(resp){
//Do something with the data
$scope.users = resp;
})
$resource
is meant to be used when communicating with RESTful apis. What your getUsers()
is doing is actually returning a resource-object, upon which you can then call get()
. But I recommend using $http
in this case.
$resource应该在与RESTful api通信时使用。getUsers()所做的实际上是返回一个resource对象,然后可以在这个对象上调用get()。但是我建议在这种情况下使用$http。
#3
0
If you want to use $resouce then you need to create two functions in your controller/factory where "templatesSuccess" return data of request.
如果您想使用$resouce,那么需要在控制器/工厂中创建两个函数,其中“templatesSuccess”返回请求数据。
getAllTemplates: function(query) {
return $resource(CONST.CONFIG.BASE_URL + 'receiver/get-templates').get(query, obj.templatesSuccess).$promise;
},
templatesSuccess: function(response) {
obj.allTemplates = response;
},