![](/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.