在angular2中使用http.post时出错

时间:2017-06-29 11:41:30

标签: angular angular-services

我收到400(错误请求)错误。 方法Task.Runsearch($event,'btn')中的点击按钮上调用,该按钮稍后调用一些服务方法来获取数据。

此处,abc.component.html方法提供数据,getFilteredData()方法未提供数据但提供400错误。Here is the image showing error

abc.component.html

searchData()

json-data.service.ts

<input [(ngModel)]="searchData" class="form-control"  type="text" placeholder="Search Keywords : 'Lenovo', 'May', 'New York'" style="height: 30px;" />
<input  type="checkbox" id="chckbox" [(ngModel)]="checked" class="form-control"  style="width: 34px;height: 32px;margin:0px;" #chkbox checked>

<button (click)="search($event,'btn')" class="btn btn-success" style="height:30px;padding: 0px 30px">Search </button>

abc.component.ts

searchData(searchText:JsonData): Observable<JsonData[]> {
            console.log('Retriving Data from Solr Server.......');
            let headers = new Headers({ 'Content-Type': 'application/json' });
            let options = new RequestOptions({ headers: headers });
            let url = "http://192.10/PIdString";
            return this.http.post(url,searchText,options).map((res: Response) => res.json()).catch(this.handleError);
          }

getFilteredData(searchText:JsonData): Observable<JsonData[]> {
                console.log('Retriving Data from Solr Server.......' + JSON.stringify(searchText));
                let headers = new Headers({ 'Content-Type': 'application/json' });
                let options = new RequestOptions({ headers: headers });
                let url = "http://1921.le"; //Local Server
                return this.http.post(url, searchText,options).map((res: Response) => res.json()).catch(this.handleError);
              }

1 个答案:

答案 0 :(得分:0)

我看不出你的JSONData是什么样的,所以我只是猜测。

  • 您的模型searchData似乎是基于<input>占位符的字符串。
  • 您的组件将searchData原样传递给您的服务功能。
  • 您的服务功能会直接传递searchData,因为它直接作为您的POST有效负载。

您的端点可能需要JSON字符串,而不是普通字符串。

相关问题