2015-09-03 49 views
0

我正在跟隨this tutorial,但我似乎無法獲得JSON內容加載成功。簡單的Spring/AngularJS休息消耗教程不會獲取/顯示數據

下面是我按照教程中創建的文件:

的index.html:

<!doctype html> 
<html ng-app> 
    <head> 
     <title>Hello AngularJS</title> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script> 
     <script src="hello.js"></script> 
    </head> 

    <body> 
     <div ng-controller="Hello"> 
      <p>The ID is {{greeting.id}}</p> 
      <p>The content is {{greeting.content}}</p> 
     </div> 
    </body> 
</html> 

hello.js:

function Hello($scope, $http) { 
    $http.get('http://rest-service.guides.spring.io/greeting'). 
     success(function(data) { 
      $scope.greeting = data; 
     }); 
} 

app.groovy:

@Controller class JsApp { } 

這裏是我的輸出

Microsoft Windows [Version 6.1.7601] 
Copyright (c) 2009 Microsoft Corporation. All rights reserved. 

C:\Dev\rest-test>spring run app.groovy 

    . ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::  (v1.2.5.RELEASE) 

2015-09-02 22:52:00.032 INFO 1968 --- [  runner-0] o.s.boot.SpringApplicat 
ion    : Starting application on asbs-dev with PID 1968 (C:\Users\me\.m2\repository\org\springframework\boot\spring-boot\1.2.5.RELEASE\spring-b 
oot-1.2.5.RELEASE.jar started by me in C:\Dev\rest-test) 
2015-09-02 22:52:00.513 INFO 1968 --- [  runner-0] ationConfigEmbeddedWebA 
pplicationContext : Refreshing org.springframework.boot.context.embedded.Annotat 
[email protected]: startup date [Wed Sep 02 22:52:0 
0 EDT 2015]; root of context hierarchy 
2015-09-02 22:52:03.049 INFO 1968 --- [  runner-0] o.s.b.f.s.DefaultListab 
leBeanFactory  : Overriding bean definition for bean 'beanNameViewResolver': 
replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; auto 
wireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBea 
nName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$White 
labelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodN 
ame=null; destroyMethodName=(inferred); defined in class path resource [org/spri 
ngframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorView 
Configuration.class]] with [Root bean: class [null]; scope=; abstract=false; laz 
yInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary= 
false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConf 
iguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver 
; initMethodName=null; destroyMethodName=(inferred); defined in class path resou 
rce [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAu 
toConfigurationAdapter.class]] 
2015-09-02 22:52:05.578 INFO 1968 --- [  runner-0] s.b.c.e.t.TomcatEmbedde 
dServletContainer : Tomcat initialized with port(s): 8080 (http) 
2015-09-02 22:52:05.983 INFO 1968 --- [  runner-0] o.apache.catalina.core. 
StandardService : Starting service Tomcat 
2015-09-02 22:52:05.987 INFO 1968 --- [  runner-0] org.apache.catalina.cor 
e.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.23 
2015-09-02 22:52:06.200 INFO 1968 --- [ost-startStop-1] org.apache.catalina.loa 
der.WebappLoader : Unknown loader org.springframework.boot.cli.compiler.Extende 
[email protected] class org.springframewo 
rk.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader 
2015-09-02 22:52:06.279 INFO 1968 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[loc 
alhost].[/]  : Initializing Spring embedded WebApplicationContext 
2015-09-02 22:52:06.279 INFO 1968 --- [ost-startStop-1] o.s.web.context.Context 
Loader   : Root WebApplicationContext: initialization completed in 5769 
ms 
2015-09-02 22:52:08.356 INFO 1968 --- [ost-startStop-1] o.s.b.c.e.ServletRegist 
rationBean  : Mapping servlet: 'dispatcherServlet' to [/] 
2015-09-02 22:52:08.370 INFO 1968 --- [ost-startStop-1] o.s.b.c.embedded.Filter 
RegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 
2015-09-02 22:52:08.371 INFO 1968 --- [ost-startStop-1] o.s.b.c.embedded.Filter 
RegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 
2015-09-02 22:52:09.236 INFO 1968 --- [  runner-0] s.w.s.m.m.a.RequestMapp 
ingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.cont 
[email protected]56: startup date 
[Wed Sep 02 22:52:00 EDT 2015]; root of context hierarchy 
2015-09-02 22:52:09.453 INFO 1968 --- [  runner-0] s.w.s.m.m.a.RequestMapp 
ingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.Res 
ponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframewo 
rk.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServ 
letRequest) 
2015-09-02 22:52:09.455 INFO 1968 --- [  runner-0] s.w.s.m.m.a.RequestMapp 
ingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.spr 
ingframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web 
.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest) 
2015-09-02 22:52:09.474 INFO 1968 --- [  runner-0] o.s.w.s.c.a.WebMvcConfi 
gurerAdapter  : Adding welcome page: file:/C:/Dev/rest-test/public/index.htm 
l 
2015-09-02 22:52:09.525 INFO 1968 --- [  runner-0] o.s.w.s.handler.SimpleU 
rlHandlerMapping : Root mapping to handler of type [class org.springframework.w 
eb.servlet.mvc.ParameterizableViewController] 
2015-09-02 22:52:09.556 INFO 1968 --- [  runner-0] o.s.w.s.handler.SimpleU 
rlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class or 
g.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
2015-09-02 22:52:09.557 INFO 1968 --- [  runner-0] o.s.w.s.handler.SimpleU 
rlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.spring 
framework.web.servlet.resource.ResourceHttpRequestHandler] 
2015-09-02 22:52:09.723 INFO 1968 --- [  runner-0] o.s.w.s.handler.SimpleU 
rlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [clas 
s org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
2015-09-02 22:52:11.165 INFO 1968 --- [  runner-0] o.s.j.e.a.AnnotationMBe 
anExporter  : Registering beans for JMX exposure on startup 
2015-09-02 22:52:11.428 INFO 1968 --- [  runner-0] s.b.c.e.t.TomcatEmbedde 
dServletContainer : Tomcat started on port(s): 8080 (http) 
2015-09-02 22:52:11.429 INFO 1968 --- [  runner-0] o.s.boot.SpringApplicat 
ion    : Started application in 12.111 seconds (JVM running for 17.50 
4) 
2015-09-02 22:52:17.615 INFO 1968 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[loc 
alhost].[/]  : Initializing Spring FrameworkServlet 'dispatcherServlet' 
2015-09-02 22:52:17.616 INFO 1968 --- [nio-8080-exec-1] o.s.web.servlet.Dispatc 
herServlet  : FrameworkServlet 'dispatcherServlet': initialization started 

2015-09-02 22:52:17.721 INFO 1968 --- [nio-8080-exec-1] o.s.web.servlet.Dispatc 
herServlet  : FrameworkServlet 'dispatcherServlet': initialization complet 
ed in 102 ms 

其他細節:命令spring run app.groovy的運行

  • Windows 7的
  • 谷歌瀏覽器

任何想法,爲什麼JSON不會在顯示HTML文件?

回答

0

當我試圖在Firefox中的例子,我在我的瀏覽器控制檯執行以下操作:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://rest-service.guides.spring.io/greeting. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

我相信這是因爲你的Groovy服務器在運行localhost或大於rest-service.guides.spring.io其他地方?默認情況下,Web瀏覽器將阻止來自不同域的$ http響應。您需要確保遠程服務器允許交叉源請求,這會反過來告訴您的瀏覽器該響應是一個不會被阻止的有效的跨源請求。

您鏈接的教程似乎指向另一個關於如何在Groovy服務器中啓用CORS的教程,雖然URL已經死了。我在github中四處看了一下,似乎教程已移至https://github.com/spring-guides/gs-rest-service-cors