简体   繁体   English

AngularJS-为什么我不能通过$ http.get获取JSON?

[英]AngularJS - Why I can't get the JSON by $http.get?

I'm developing a web application in java with NetBeans using AngularJS. 我正在使用AngularJS和NetBeans在Java中开发Web应用程序。

When I'm accessing my WebService in localhost I'm getting the JSON array with the objects that I need, working very well 当我在本地主机中访问WebService时,我得到了带有所需对象的JSON数组,并且运行良好

BUT

in the controller, I'm not getting the information 在控制器中,我没有得到信息

Log of the Web browser: Web浏览器的日志:

Result: [object Object] OR {} script.js:140:5 结果:[object Object]或{} script.js:140:5

Success/Error: undefined 成功/错误:未定义

Code: 码:

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);
});

And if I put this code in the view I got an error: 而且,如果我将此代码放在视图中,则会出现错误:

<div ng-bind="$scope.result"></div>

Error: $scope.result is not defined 错误:未定义$ scope.result

I have configured the $routeProvider and is absolutely correct 我已经配置了$ routeProvider并且绝对正确

Thanks a lot <3 Big Hug! 非常感谢<3 Big Hug!

You can try in the following way. 您可以按照以下方式尝试。

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;
        });

    }
});

And use this html to display error 并使用此html显示错误

<div class="error">{{error}}</div>

You need to assign your response to controller scope variable result as a result of asynch request like this 由于这样的请求,您需要将响应分配给控制器作用域变量结果

 $scope.result = success

MoreOver you can avoid using var when declaring $scope variables 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);

}); });

also in view 还在看

 <div ng-bind="result"></div>

no need of $scope 不需要$ scope

Try <div ng-model="result"></div> for the second error. 尝试<div ng-model="result"></div>处理第二个错误。

And no need to do: 无需这样做:

$scope.medicoSelecionado;
$scope.aux;
$scope.result;

Just use a new model when you need one; 只需在需要时使用新模型即可; no declaration is needed. 无需声明。

Doing $scope.result = success in your .then() should be fine, as suggested by Vinod Louis. 如Vinod Louis所建议,在$scope.result = success ()中执行$scope.result = success应该没问题。

The way I would do it: 我会做的方式:

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));

});

What does aux do by the way? aux作用是什么?

You have to define var aux = {} because if you not defined anything then it will show undefined 您必须定义var aux = {}因为如果您未定义任何内容,那么它将显示未定义
and you are getting object in success so that it is showing [object, object] 并且您成功地获取了对象,从而显示了[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);
});

GOT! 得到!

For some reason was having conflict with the route 'login'. 由于某种原因与路由“登录”发生冲突。 I don't known why. 我不知道为什么。

The solution was deleting the redirectTo line 解决方案是删除redirectTo行

when('/inicioMedico', {
     templateUrl: 'inicioMedico.html',
     controller: "inicioMedicoCTRL"
}).
otherwise ({
    // redirectTo: 'login' ERROR HERE
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM