【问题标题】:How do I send this json request body from angular using http client post method?如何使用 http 客户端发布方法从角度发送此 json 请求正文?
【发布时间】:2021-12-12 09:25:35
【问题描述】:

这是请求正文的 Postman 示例 我将 query_string 发送到烧瓶后端,它返回推文列表。

现在我想使用 http 客户端 post 方法从 Angular 应用程序发送 post 请求

searchTerm 是查询字符串

import { GoogleResponse } from './components/GoogleResponse.model';
import { environment } from './../environments/environment';
import { Injectable, Query } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { solr_Response } from './components/solr_response.model';

@Injectable({
  providedIn: 'root'
})
export class SearchService {

  

  private url: string = '3.22.175.82:5000/queryresult';



  /*     solr url     */


  constructor(private httpClient:HttpClient) { }

  /**
   * 
   * @param searchTerm 
   * @returns 
   */
  getSearchData(searchTerm: string): Observable<any> {

    
    
    return this.httpClient.post(this.url, searchTerm);
  
  }



}


我只是发送一个字符串作为请求正文,但它给了我错误我的猜测是正文需要在一个对象中,或者简单的字符串就足够了。 但我收到了这个错误

【问题讨论】:

  • 网址不应该以 http:// 或 https:// 开头吗?
  • 在您的错误响应中,url 字段显示的 URL 不正确。

标签: angular


【解决方案1】:

正如@vicatcu 所述,url 应该是一个以 http:// 或 https:// 开头的有效 URL。此外,您的端点需要一个对象{ "query_string": "..." }。因此它应该是这样的:

private url = 'http://3.22.175.82:5000/queryresult';

...

getSearchData(searchTerm: string): Observable<any> {
  return this.httpClient.post(this.url, { query_string: searchTerm });
}

【讨论】:

    猜你喜欢
    • 2013-02-25
    • 2019-12-04
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    相关资源
    最近更新 更多