【发布时间】:2019-12-17 02:10:06
【问题描述】:
我正在使用带有 REST api 的 Angular 连接到后端。我正在传递用户名和密码以访问资源,servletFilter 看起来很好,问题出在浏览器发送响应时。生成此错误。我已经阅读了很多这样的问题,但找不到能解决我问题的问题
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
这是登录服务
constructor (private http : HttpClient){}
sendCredential(username: string, password: string){
let url = "http://localhost:8080/index";
let params = 'username='+username+'&password='+password;
let headers = new HttpHeaders(
{
'Content-Type': 'application/x-www-form-urlencoded'
// 'Access-Control-Allow-Credentials' : true
});
return this.http.post(url,params,{headers: headers, withCredentials : true}).pipe(res => {
return res;
});
这里是调用登录服务方法的登录组件方法
onSubmit() {
this.loginService.sendCredential(this.username, this.password).subscribe(
res => {
this.loggedIn=true;
localStorage.setItem('PortalAdminHasLoggedIn', 'true');
location.reload();
},
err => console.log(err)
);
}
感谢您的帮助
【问题讨论】:
-
首先您在开发者工具下的网络选项卡中检查响应类型。如果不是 json,则准备 json 格式的服务器端数据。
标签: angular rest spring-boot servlets