我完全关注this tutorial,但我似乎无法成功加载JSON内容。
以下是我根据教程创建的文件:
的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 { }
以下是我运行命令spring run app.groovy
的输出:
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
ionConfigEmbeddedWebApplicationContext@1336a56: 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
dGroovyClassLoader$DefaultScopeParentClassLoader@1adac6c 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
ext.embedded.AnnotationConfigEmbeddedWebApplicationContext@1336a56: 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
其他详细信息:
有关为什么JSON不会在HTML文件中显示的任何想法?
答案 0 :(得分: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