Angular Http解析期间Http失败
我正在使用Angular和REST api连接到后端。我正在传递用户名和密码以访问资源,servletFilter看起来很好,问题是在浏览器发送响应时。此错误已生成。我读过很多这样的问题,但我找不到一个能解决我问题的Angular Http解析期间Http失败,angular,rest,spring-boot,servlets,Angular,Rest,Spring Boot,Servlets,我正在使用Angular和REST api连接到后端。我正在传递用户名和密码以访问资源,servletFilter看起来很好,问题是在浏览器发送响应时。此错误已生成。我读过很多这样的问题,但我找不到一个能解决我问题的 HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8080/front", ok: false, …} error: error: Synt
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK",
url: "http://localhost:8080/front", ok: false, …}
error:
error: SyntaxError: Unexpected token < in JSON at position 0 at
JSON.parse (<anonymous>) at XMLHttpRequest.onLoad
(http://localhost:4200/vendor.js:10132:51) at ZoneDelegate.invokeTask
(http://localhost:4200/polyfills.js:3626:31) at Object.onInvokeTask
text: "
__proto__: Object
headers: HttpHeaders
lazyInit: () => {…}
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure during parsing for
http://localhost:8080/front"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8080/front"
__ proto__: HttpResponseBase
下面是调用登录服务方法的登录组件方法
onSubmit() {
this.loginService.sendCredential(this.username, this.password).subscribe(
res => {
this.loggedIn=true;
localStorage.setItem('PortalAdminHasLoggedIn', 'true');
location.reload();
},
err => console.log(err)
);
}
感谢帮助当来自REST服务的响应返回以尖括号开头的html页面时,会出现此错误,这就是此错误的原因
Unexpected token < in JSON at position 0 at
JSON中位置0处的意外标记<
JSON.parse()
要使响应不抛出错误,必须将响应设置为来自服务端的JSON响应,该响应以JSON样式的花括号开头{
您可以通过打开“开发人员工具”和“网络”选项卡来检查响应。然后请求登录,然后单击该记录并查看其在“响应”选项卡右侧显示的详细信息。您将发现一个html页面,这可能是因为服务器的响应未返回有效的JSON。
检查您的网络选项卡,查看传入的服务器响应。因为我正在使用x-www-form-urlencoded,我应该做什么不是从您这边,而是从后端,服务将返回Json而不是Html页面。请检查服务后端并使其返回Json。这正是问题所在,但如何在中将Html转换为Json取决于服务器端使用哪种语言的服务器。我使用的是java Spring Boot。首先,您可以在“开发人员工具”下的“网络”选项卡中检查响应类型。如果不是json,则以json格式准备服务器端数据。
Unexpected token < in JSON at position 0 at