Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 向API发送Http请求,对象作为参数_Angular_Api_Http_Post_Get - Fatal编程技术网

Angular 向API发送Http请求,对象作为参数

Angular 向API发送Http请求,对象作为参数,angular,api,http,post,get,Angular,Api,Http,Post,Get,我有一个搜索表单,可以在web应用程序中填写输入字段。这些输入字段将在我的SearchCriteria对象中结束 单击search按钮后,我想将SearchCriteria对象发送到API,API将返回匹配的Order对象的集合。既然后端代码都准备好了,我只需要执行正确的API调用 这是我尝试过的,但我似乎总是遇到http 400错误 getOrders(searchCriteria: SearchCriteria): Observable<Order[]> { let

我有一个搜索表单,可以在web应用程序中填写输入字段。这些输入字段将在我的SearchCriteria对象中结束

单击search按钮后,我想将SearchCriteria对象发送到API,API将返回匹配的Order对象的集合。既然后端代码都准备好了,我只需要执行正确的API调用

这是我尝试过的,但我似乎总是遇到http 400错误

  getOrders(searchCriteria: SearchCriteria): Observable<Order[]> {

    let requestoptions = {
      'headers': new HttpHeaders({'Content-Type': 'application/json'}),
      'withCredentials': true
    };

    return this.http.post(environment.backend + "/api/orders/getOrders", searchCriteria, requestoptions).pipe(map((response: any) => {
      return response;
    }));
  }
/api/orders/getOrders看起来像:

@RequestMapping(path = "getOrders", method = POST, consumes = "application/json", produces = "application/json")
public Collection<Order> getOrders(@RequestBody SearchCriteria searchCriteria) {
    return orderService.getOrders(searchCriteria);
}
@RequestMapping(path=“getOrders”,method=POST,consumes=“application/json”,products=“application/json”)
公共集合getOrders(@RequestBody SearchCriteria SearchCriteria){
returnorderservice.getOrders(searchCriteria);
}
现在我一直在读东西,但一直没有弄明白

我实际上想要(我想)的是在我的http请求的请求体中发送对象(searchCriteria),所以URL看起来没有那么长,当然http请求是成功的

我做错了什么?有什么更好的吗?

像这样试试

   getOrders(searchCriteria: SearchCriteria): Observable<Order[]> {
          let headers = new HttpHeaders({'Content-Type': 'application/json'}),
          return this.http.post(environment.backend + "/api/orders/getOrders",searchCriteria, {headers})
          .pipe(map((response: any) => {
            return response;
           }));
      }
getOrders(searchCriteria:searchCriteria):可观察{
let headers=new-HttpHeaders({'Content-Type':'application/json'}),
返回此.http.post(environment.backend+“/api/orders/getOrders”,searchCriteria,{headers})
.pipe(map)(响应:any)=>{
返回响应;
}));
}

通过API发送对象需要stringify吗?@Mridul:不,这只是我的一次尝试。我编辑了我的帖子,并添加了特定的API方法,如果这可能更有意义的话。你不能在GET请求中发送消息正文。如果API接受POST,则使用
http.POST
。否则,使用JavaScriptPost将对象映射到参数POST中的第二个参数应该是消息体而不是选项如果可以使用POST,请从
requestoptions
中删除
params
,然后尝试
POST(url,JSON.stringify(object),requestoptions)
   getOrders(searchCriteria: SearchCriteria): Observable<Order[]> {
          let headers = new HttpHeaders({'Content-Type': 'application/json'}),
          return this.http.post(environment.backend + "/api/orders/getOrders",searchCriteria, {headers})
          .pipe(map((response: any) => {
            return response;
           }));
      }