![](/img/trans.png)
[英]Can't get JSON data from 000webhost server with AngularJS $http.get
[英]AngularJS - Why I can't get the JSON by $http.get?
我正在使用AngularJS和NetBeans在Java中开发Web应用程序。
当我在本地主机中访问WebService时,我得到了带有所需对象的JSON数组,并且运行良好
但
在控制器中,我没有得到信息
Web浏览器的日志:
结果:[object Object]或{} script.js:140:5
成功/错误:未定义
码:
minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) {
$scope.medicoSelecionado;
var aux;
var $scope.result = window.console.log($http.get("http://localhost:8080/Clinica5/webresources/medicos")).then(function (success) {
aux = success;
}, function (error) {
aux = error;
});
window.console.log("Result: "+$scope.result+ " OR "+JSON.stringify($scope.result));
window.console.log("Success/Error: "+aux);
});
而且,如果我将此代码放在视图中,则会出现错误:
<div ng-bind="$scope.result"></div>
错误:未定义$ scope.result
我已经配置了$ routeProvider并且绝对正确
非常感谢<3 Big Hug!
您可以按照以下方式尝试。
minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) {
$scope.functionName = function(){
//define
$scope.medicoSelecionado = {};
$http.get("http://localhost:8080/Clinica5/webresources/medicos").then(function (success) {
console.log(success);
//success data passed
$scope.medicoSelecionado = success;
}, function (error) {
console.log(error);
//error message
$scope.error = error;
});
}
});
并使用此html显示错误
<div class="error">{{error}}</div>
由于这样的请求,您需要将响应分配给控制器作用域变量结果
$scope.result = success
MoreOver可以在声明$ scope变量时避免使用var
minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) {
$scope.medicoSelecionado;
$scope.aux = {};
$scope.result {};
$http.get("http://localhost:8080/Clinica5/webresources/medicos").then(function (success) {
$scope.result = success;
}, function (error) {
$scope.aux = error;
});
window.console.log("Result: ",$scope.result, " OR ",JSON.stringify($scope.result));
window.console.log("Success/Error:",$scope.aux);
});
还在看
<div ng-bind="result"></div>
不需要$ scope
尝试<div ng-model="result"></div>
处理第二个错误。
无需这样做:
$scope.medicoSelecionado;
$scope.aux;
$scope.result;
只需在需要时使用新模型即可; 无需声明。
如Vinod Louis所建议,在$scope.result = success
()中执行$scope.result = success
应该没问题。
我会做的方式:
minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) {
$http.get("http://localhost:8080/Clinica5/webresources/medicos")
.then(function (success) {
$scope.result = success;
}, function (error) {
$scope.result = error;
});
window.console.log("Result: "+$scope.result+ " OR "+JSON.stringify($scope.result));
});
aux
作用是什么?
您必须定义var aux = {}
因为如果您未定义任何内容,那么它将显示未定义
并且您成功地获取了对象,从而显示了[object, object]
minhaAplicacao.controller('inicioPacienteCTRL', function ($scope, $http) {
$scope.medicoSelecionado;
var aux = {};
var $scope.result = window.console.log($http.get("http://localhost:8080/Clinica5/webresources/medicos")).then(function (success) {
aux = success;
}, function (error) {
aux = error;
});
window.console.log("Result: "+$scope.result+ " OR "+JSON.stringify($scope.result));
window.console.log("Success/Error: "+aux);
});
得到!
由于某种原因与路由“登录”发生冲突。 我不知道为什么。
解决方案是删除redirectTo行
when('/inicioMedico', {
templateUrl: 'inicioMedico.html',
controller: "inicioMedicoCTRL"
}).
otherwise ({
// redirectTo: 'login' ERROR HERE
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.