2017-01-10 2 views
0

Ich habe zwei Dienste getCountUp und getCountDown. Ich möchte JSON-Daten von beiden Diensten beziehen.AngularJS bekomme JSON-Daten von mehr als einem Service

Mein Controller-Code unten

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

In meiner Konsole gegeben Ich erhalte die folgenden Fehler

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":""} 

Aber das funktioniert nicht. Ich bekomme immer JSON-Daten von nur einem Dienst.

+0

ich nichts falsch hier sehen ... Warum nicht Fehler Rückrufe in den Versprechungen versuchen setzen, um zu sehen, ob vielleicht gibt es andernfalls jeden Anruf? Oder können Sie im Browser-Netzwerk-Debugger überprüfen, ob die Anrufe gesendet werden? – olivarra1

+0

Welche Daten erhalten Sie von welchem ​​Dienst - Url oder Url1? – Subhajit

+0

@Subhajit Von servicesUrl. – user3441151

Antwort

1

Das Problem scheint auf der Server-Seite zu sein, wie es nicht mehrere Anfragen gleichzeitig behandeln kann oder etwas (was im Allgemeinen ziemlich schlecht ist). Du solltest damit wahrscheinlich umgehen.

Sie umgehen das Problem wahrscheinlich nur zu einem Zeitpunkt eine Serveranfrage zu tun wie folgt:

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

In diesem Fall Wenn ich eine Ausnahme von ServicesUrl bekomme, bekomme ich auch keine Daten von ServicesUrl1? – user3441151

+0

@ user3441151 Dann auch 'return getHostStatusService.getResponders (servicesUrl1)' in den Fehler Rückruf des ersten '.then (' – olivarra1

+0

Aber @fissio Rat: Etwas wirklich falsch passiert auf dem Server. Sie sollten versuchen, diese Seite zu beheben – olivarra1

0

Sie haben mehrere return-Anweisung in der Factory-Methode hergestellt

Verwenden Sie den Code unten, für die Demo-Zweck Ich habe JSON-Dateien verwendet.

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

+0

Obwohl es ein Code-Geruch ist, ist es wirklich egal.Die zweite Rückkehr wird nie aufgerufen werden – olivarra1

+0

Ich denke, dass Sie das Fabrik-Konzept in eckigen wissen, die eine Rückkehr-Aussage erzwingt, wenn nicht bitte verweisen Sie es Also wird die zweite Anweisung force ausgeführt – Aravind

+1

Mit allem Respekt, AngularJS ist ein JavaScript-Framework und ändert nicht die Art und Weise wie JS ausgeführt wird Wenn Sie in JS in einer Funktion 'return 1; return 2; hole '1' als Rückgabewert, und' return2' Anweisung wird niemals ausgeführt. Versuchen Sie, eine 'console.log' dazwischen zu legen, sogar in einer AngularJS Anwendung. – olivarra1

Verwandte Themen