0
我得到400(错误请求)错误。 方法search($event,'btn')
被点击abc.component.html
中的按钮调用,该按钮稍后调用一些服务中的方法来获取数据。错误在角度2中使用http.post
这里,getFilteredData()
方法提供的数据,而searchData()
方法不提供数据,但提供400错误。 Here is the image showing error
abc.component.html
<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>
JSON-data.service.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);
}
abc.component.ts
checked:boolean = true;
constructor(public jsonDataService: JsonDataService, public injector: Injector) { }
search(event, from) {
if(this.checked){
alert("checked");
}
else{
alert("unchecked");
}
if (this.searchData === "" || this.searchData === null) {
alert("Enter any keyword to search !");
}else if(this.checked === true && this.searchData === "" || this.searchData === null){
alert("Enter any keyword to search !");
}else if(this.checked !== true && this.searchData === "" || this.searchData === null){
alert("Enter any keyword to search !");
}else if(this.checked === true && (this.searchData !== "" || this.searchData !== null)){
console.log("checked");
console.log("Searching.......");
this.jsonDataService.getFilteredData(this.searchData).subscribe(
success => this.buildDataUI1(success),
error => this.errorMessage = <any>error);
}
else if(this.checked === false && (this.searchData !== "" || this.searchData !== null)){
console.log("unchecked");
console.log("Searching.......");
this.jsonDataService.searchData(this.searchData).subscribe(
success => this.buildDataUI1(success),
error => this.errorMessage = <any>error);
}
}
错误400意味着您不发送后端epxects的有效负载。你的后端期望什么,你准确发送了什么?你可以在网络标签 – trichetriche
下的The Chome控制台中看到它。其实,我是angular2的新手。这里有一个方法是获取数据,其他方法是给出错误,因为您可以看到除url请求外,这两种方法都相同。 –
它们是相同的,但它们不会调用相同的端点。正如我刚刚所述,端点返回错误400,这意味着您不发送正确的数据。所以,请发布前端发送的内容以及后端接受的内容。 – trichetriche