【问题标题】:Http response error when sending a post request Angular 4发送发布请求Angular 4时的Http响应错误
【发布时间】:2018-08-19 01:30:37
【问题描述】:

即使我将responseType 作为'text' 放在标题中,当收到来自服务器的响应时,我也会收到此错误:

   HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "https://beta.rokhas.ma/karazortal/web.jsp", ok: false, …}
error
:
{error: SyntaxError: Unexpected token < in JSON at position 15 at JSON.parse (<anonymous>) at XMLHtt…, text: "↵↵ ↵ ↵ ↵↵ ↵   ↵↵<!doctype html>↵ ↵↵<html lang="fr">…ight:0;border:0"></iframe>↵</body> ↵    ↵</html>↵"}
headers:HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message:"Http failure during parsing for https://beta.rokhas.ma/karazortal/web.jsp"
name: "HttpErrorResponse"
ok:false
status:200
statusText: "OK"
url : "https://beta.rokhas.ma/karazortal/web.jsp"
__proto__ : HttpResponseBase

我想在我的应用中显示此 HTML 正文内容。

myservice.ts

logIn(user){
    let body = new URLSearchParams();
    body.set('j_username', user.email);
    body.set('j_password', user.password);
    let headers= new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'})
    headers.append('responseType', 'text');
    const httpOptions = {
      headers: headers  
    };   
    console.log(body.toString());  
    return this.http.post(this.API_URL+'karazortal/kas/signin',  
     body.toString(),  httpOptions)

  }

【问题讨论】:

标签: angular http-error


【解决方案1】:

这对我有用。请检查一下。

logIn(user) {
  let body = new URLSearchParams();
  body.set('j_username', user.email);
  body.set('j_password', user.password);
  let headers= new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
  const httpOptions = {
    headers: headers
  };
  console.log(body.toString());
  return this.http.post(this.API_URL+'karazortal/kas/signin', body.toString(),{responseType: 'text'},  httpOptions)
}

【讨论】:

    猜你喜欢
    • 2018-11-28
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多