我正在处理angular2,我遇到了HTTP post响应问题。我的端点应返回布尔值并在chrome的网络响应选项卡中正确返回响应,但是当我在控制台中记录响应时,它会显示空对象。我无法弄清楚为什么网络和控制台选项卡中的响应不同。 这是我的代码:
this._buybackApi.checkValueUnique(checkValueData, CheckValueFormData).subscribe(
(response) => {
console.log(response);
this.checkUniqueRes = response;
},
(error) => {
console.log(error);
},
() => {
console.log(this.checkUniqueRes);
}
);
Code in api file:
public checkValueUnique(uniqueValue: models.UniqueValue, extraHttpRequestParams?: any): Observable<boolean> {
return this.checkValueUniqueWithHttpInfo(uniqueValue, extraHttpRequestParams)
.map((response: Response) => {
if (response.status === 204) {
return undefined;
} else {
return response.json() || {};
}
});
}
public checkValueUniqueWithHttpInfo(uniqueValue: models.UniqueValue, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + '/IsValueUnique';
let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON());
// verify required parameter 'uniqueValue' is not null or undefined
if (uniqueValue === null || uniqueValue === undefined) {
throw new Error('Required parameter uniqueValue was null or undefined when calling checkValueUnique.');
}
// to determine the Content-Type header
let consumes: string[] = [
'*/*'
];
// to determine the Accept header
let produces: string[] = [
'application/json'
];
// authentication (basicAuth) required
// http basic authentication required
if (this.configuration.username || this.configuration.password) {
headers.set('Authorization', 'Basic ' + btoa(this.configuration.username + ':' + this.configuration.password));
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: extraHttpRequestParams
});
if (extraHttpRequestParams) {
requestOptions = (<any>Object).assign(requestOptions, extraHttpRequestParams);
}
return this.http.request(path, requestOptions);
}
非常感谢任何帮助。
答案 0 :(得分:1)
根据您提供的信息,checkValueUnique
的退货声明将打印以下三个值中的一个:
undefined
如果是204响应状态代码。{}
未定义,则为空对象response.json()
。您收到{}
,因为对response.json()
的调用未定义。你说响应返回一个布尔值。响应正文中的布尔值(true或false)无效JSON。