【问题标题】:Angular Http failure during parsing for url解析 url 期间的 Angular Http 失败
【发布时间】:2019-05-13 09:44:32
【问题描述】:

我在使用 Angular 中的 http.post 方法发布表单数据时出错。我将表单数据成功存储到数据库中,但 http 返回如下解析错误:

HttpErrorResponse
error: {error: SyntaxError: Unexpected token I in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttp…, text: "Inserted Successfully"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:8080/api/post"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"

我无法弄清楚问题出在哪里。 API 是用 Spring Boot 编写的 这是我的代码

组件.ts

details: UserDetails = {
    firstName: '',
    lastName: '',
    email: '',
    adress: '',
    city: '',
    state: ''
  };

    public sendDetails(): void {
          const url  = 'http://localhost:8080/api/post';
          const headers = new HttpHeaders().set('Content-Type', 'application/json');
          this.http.post(url, JSON.stringify(this.details), {headers : headers} ).subscribe(
            data => {
              console.log(data);
            },
            err => {
              console.error(err);
            }
          );
      }

Service.java

public String postData(Map map) throws SQLException {
    Connection conn = null;
    CallableStatement cs = null;
    try {
      conn = ds.getConnection();
      cs = conn.prepareCall("Begin leo_pack.leo_postUserData1(?, ?, ?, ?, ?, ?); End;");
      cs.setString(1, String.valueOf(map.get("firstName")));
      cs.setString(2, String.valueOf(map.get("lastName")));
      cs.setString(3, String.valueOf(map.get("email")));
      cs.setString(4, String.valueOf(map.get("adress")));
      cs.setString(5, String.valueOf(map.get("city")));
      cs.setString(6, String.valueOf(map.get("state")));

      cs.execute();
      return "Inserted Successfully";

    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      if (cs != null) cs.close();
      if (conn != null) ds.evictConnection(conn);
    }
    return null;
  }

Controller.java

   @PostMapping(value = "/post", consumes = MediaType.APPLICATION_JSON_VALUE)
    private String test(@RequestBody Map map, BindingResult binding) throws SQLException, ValidationException {

        if(binding.hasErrors()){
            throw new ValidationException("error !!! ");
        }
        return service.postData(map);
    }

【问题讨论】:

    标签: angular spring http spring-boot angular-httpclient


    【解决方案1】:

    this.details 不需要被字符串化。将Content-Type 标头设置为application/json 也是多余的。

    试试这个:

    constructor(private http: HttpClient, ...) {}
    
    ...
    
    public sendDetails(): void {
      const url = 'http://localhost:8080/api/post';
      this.http.post(url, this.details).subscribe(
        data => {
          console.log(data);
        },
        err => {
          console.error(err);
        }
      );
    }
    

    【讨论】:

    • @MashrabjonAkbarov,您能否在通过 Postman 向它发送请求时检查此 API 是否正常工作?
    • 是的,它工作正常,唯一不能正常工作的是 Angular 中的 http post 方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2020-06-03
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多