【问题标题】:Error occurred :Http failure during parsing for [URL] while calling http Post from angular 7发生错误:从角度 7 调用 http Post 时解析 [URL] 时出现 Http 失败
【发布时间】:2019-05-16 15:07:21
【问题描述】:

我正在尝试将表单中的数据发布到我的 REST 后端服务器,正在发布数据,但 httpclient.post 返回错误响应“发生错误:解析 [URL] 期间的 Http 失败”。

我已经尝试过类似问题所建议的修复,但没有找到任何适合 angular 7 的修复

import { Injectable } from '@angular/core';
import * as UrlConstants from './urls';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { Book } from './book/book';
import { catchError } from 'rxjs/operators';

const httpOptions = {
  headers: new HttpHeaders().set('Content-Type','application/json')
};
//import { RequestOptions, Request, RequestMethod } from '@angular/http';
@Injectable({
  providedIn: 'root'
})
export class BookService {
  private _getBooksUrl: string = UrlConstants.BOOKS_URL;
  private _postBooksUrl: string = UrlConstants.POST_BOOK_URL;
  constructor(private _http: HttpClient) { }

  getAllBooks(): Observable<Book[]> {
    console.log(this._getBooksUrl);
    return this._http.get<Book[]>(this._getBooksUrl)
      .pipe(catchError(this.errorHandler));
  }

  addBook(book: Book) {
    let body = JSON.stringify(book);
    return this._http.post<Book>(this._postBooksUrl, body, httpOptions)
    .pipe(catchError(this.errorHandlerPost));
  }
  errorHandler(errorHandler: HttpErrorResponse): Observable<Book[]> {
    return throwError(errorHandler.message || "server error");
  }
  errorHandlerPost(errorHandler: HttpErrorResponse) {
    return throwError(errorHandler.message || "server error");
  }
}

bookcomponent.ts

export class BookComponent implements OnInit {
  public books = [];
  public errorMessage;
  book = new Book();
  constructor(private _bookService: BookService) { }

  ngOnInit() {
    this.getBooks();
  }
  getBooks():void{
    this._bookService.getAllBooks()
    .subscribe(
      data => {
        this.books = data;
        console.log(data)
      },
      error => {
        this.errorMessage = error
        console.log(error);
      }
    );
  }

  addBook():void{
    this._bookService.addBook(this.book)
    .subscribe(
      (response) => {
        console.log(response)
      },
      (error) => {
        console.log("Error occurred :" + error);
      }
    );
  }

}

【问题讨论】:

  • 你也可以发UrlConstants吗?
  • post 中传递了什么httpOptions?
  • 能否请您也发布控制台中发生的任何错误
  • const httpOptions = { headers: new HttpHeaders().set('Content-Type','application/json') };
  • 发生错误:解析localhost:8080/BookAPI/api/create时的Http失败

标签: angular


【解决方案1】:

请先试试这个。如果您已经尝试过这种方法,请告诉我

更改return this._http.post&lt;Book&gt;(this._postBooksUrl, body, httpOptions)return this._http.post&lt;Book&gt;(this._postBooksUrl, body, {responseType: 'text'})

【讨论】:

  • 如果我更改为 { responseType : 'text' },我会收到错误“src/app/book.service.ts(28,61) 中的错误:错误 TS2322: Type '"text" ' 不能分配给类型 '"json"'。"编译时。
  • 如果您使用过postman,能否请您在postman中测试api并发布响应元数据。这将有助于解决问题
  • access-control-allow-credentials →true access-control-allow-origin →chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop 内容长度 →24 内容类型 →text/plain;charset=ISO-8859 -1 日期 →2019 年 5 月 16 日星期四 08:39:53 GMT 不同 →起源
  • 响应是:用 ID = 45 创建的书,其中 id 是主键。
  • 就是这样,返回的内容类型是text/plain。您可以尝试在后端将响应内容类型设置为 application/json 吗?无需进行我建议的 {responseType: 'text'} 更改
猜你喜欢
  • 2021-03-14
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 2020-11-10
  • 2016-04-29
  • 1970-01-01
相关资源
最近更新 更多