AngularJS从多个服务获取JSON数据

时间:2017-01-10 08:50:32

标签: angularjs

我有两个服务 getCountUp getCountDown 。我想从两个服务中获取JSON数据。

我的控制器代码如下所示

(function () {
    angular
        .module('app')
        .controller('VisitorsController', [ 'getHostStatusService', VisitorsController])
        .factory("getHostStatusService", ['$http', function($http) {
            return {
                getResponders: function(servicesUrl) {
                    return $http.get(servicesUrl).then(function(response) {
                        console.log(response.data);
                        return response.data;
                    });
                }
            };
            return getHostStatusService;
        }]);

    function VisitorsController(getHostStatusService) {
        var vm = this;
        var servicesUrl = app.serviceCallUrl+'getCountUp';

        var getCountUp = 0;
        var getCountDown = 0;

        console.log(servicesUrl);
        getHostStatusService.getResponders(servicesUrl).then(function(data) {
            console.log(data);
            getCountUp = data;
        });

        var servicesUrl1 = app.serviceCallUrl+'getCountDown';
        console.log(servicesUrl1);
        getHostStatusService.getResponders(servicesUrl1).then(function(data) {
            console.log(data);
            getCountDown = data;
        });
    }
})();

在我的控制台中,我收到以下错误

GET http://localhost:8080/getCountDown 500 ()
Possibly unhandled rejection: {"data":"<!DOCTYPE html><html><head><title>Apache Tomcat/8.5.8 - Error report</title><style type=\"text/css\">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 500 - Request processing failed; nested exception is org.hibernate.TransactionException: Transaction not successfully started</h1><div class=\"line\"></div><p><b>type</b> Exception report</p><p><b>message</b> <u>Request processing failed; nested exception is org.hibernate.TransactionException: Transaction not successfully started</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b></p><pre>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.TransactionException: Transaction not successfully started\n\torg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)\n\torg.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)\n\tjavax.servlet.http.HttpServlet.service(HttpServlet.java:622)\n\torg.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)\n\tjavax.servlet.http.HttpServlet.service(HttpServlet.java:729)\n\torg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)\n\tcom.alleviateconsultancy.nms.configuration.CORSFilter.doFilter(CORSFilter.java:25)\n</pre><p><b>root cause</b></p><pre>org.hibernate.TransactionException: Transaction not successfully started\n\torg.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:172)\n\tcom.alleviateconsultancy.nms.dao.NMSDaoImp.getServiceHostCountDown(NMSDaoImp.java:574)\n\tcom.alleviateconsultancy.nms.service.NMSDaoServiceImp.getServiceHostCountDown(NMSDaoServiceImp.java:106)\n\tsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tjava.lang.reflect.Method.invoke(Method.java:498)\n\torg.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)\n\torg.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)\n\torg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)\n\torg.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)\n\torg.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)\n\torg.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)\n\torg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)\n\torg.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)\n\tcom.sun.proxy.$Proxy41.getServiceHostCountDown(Unknown Source)\n\tcom.alleviateconsultancy.nms.controller.AppController.getHostCountDown(AppController.java:141)\n\tsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tjava.lang.reflect.Method.invoke(Method.java:498)\n\torg.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)\n\torg.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)\n\torg.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)\n\torg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)\n\torg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)\n\torg.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)\n\torg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)\n\torg.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)\n\torg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)\n\torg.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)\n\tjavax.servlet.http.HttpServlet.service(HttpServlet.java:622)\n\torg.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)\n\tjavax.servlet.http.HttpServlet.service(HttpServlet.java:729)\n\torg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)\n\tcom.alleviateconsultancy.nms.configuration.CORSFilter.doFilter(CORSFilter.java:25)\n</pre><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/8.5.8 logs.</u></p><hr class=\"line\"><h3>Apache Tomcat/8.5.8</h3></body></html>","status":500,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"http://localhost:8080/getCountDown","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}

但这不起作用。我总是只从一个服务获得JSON数据。

2 个答案:

答案 0 :(得分:1)

问题似乎是在服务器端,就像它不能同时处理多个请求或某些东西(一般来说非常糟糕)。所以你应该对此进行处理。

您可以通过一次只执行一个服务器请求来绕过问题,如下所示:

function VisitorsController(getHostStatusService) {
    var vm = this;
    var servicesUrl = app.serviceCallUrl+'getCountUp';
    var servicesUrl1 = app.serviceCallUrl+'getCountDown';

    var getCountUp = 0;
    var getCountDown = 0;

    getHostStatusService.getResponders(servicesUrl)
    .then(function(data) {
        getCountUp = data;
        return getHostStatusService.getResponders(servicesUrl1)
    })
    .then(function(data) {
        getCountDown = data;
    });
}

答案 1 :(得分:0)

您已在工厂方法

中创建了多个return语句

使用以下代码,出于演示目的,我使用了Json文件。

app.controller('VisitorsController', [ 'getHostStatusService', VisitorsController]);

  function VisitorsController(getHostStatusService) {
        var vm = this;
        var servicesUrl = 'getCountUp.json';

        var getCountUp = 0;
        var getCountDown = 0;

        console.log(servicesUrl);
        getHostStatusService.getResponders(servicesUrl).then(function(data) {
            console.log(data);
            getCountUp = data;
        });

        var servicesUrl1 = 'getCountDown.json';
        console.log(servicesUrl1);
        getHostStatusService.getResponders(servicesUrl1).then(function(data) {
            console.log(data);
            getCountDown = data;
        });
    }
app.factory("getHostStatusService", ['$http', function($http) {
            return {
                getResponders: function(servicesUrl) {
                    return $http.get(servicesUrl).then(function(response) {
                        console.log(response.data);
                        return response.data;
                    });
                }
            };
        }]);

<强> LIVE DEMO