我们正在尝试发布到WCF REST方法。我们可以使用SoapUI,Postman和YARC成功地进行POST,但是当我们在Angular 5中尝试时,我收到的是NULL对象。
客户代码:
const headers: any = {'Content-Type': 'application/json'};
return this.ajax.request<any>({
url: url,
method: method (POST),
headers: headers,
options: {
body: JSON.stringify(req.body)
}
});
}
public request<T>(req: AjaxRequest): Observable<HttpEvent> {
return this.http.request<T>(req.method, req.url, req.options)
}
//this.http = HttpClient
// Component
public onNext(event: any): void {
this._saveInfo(getInsertDmgReqPayload({
data: event.value,
dmgInfo: this.responses['dmg'],
emailId: this.util.getQueryStringValue('uname')
}));
this.util.navigate('education');
}
private _saveInfo(data: any): void {
this.appraisal.request('saveDmgInfo', null, {body: data})
.subscribe(v => {
console.log(v);
});
}
服务器端逻辑:
// WCF Interface definition
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
Result InsertDemographicInfo(Demographic d);
// Service code
public Result InsertDemographicInfo(Demographic d) // The Demographic object is null when posting from Angular
{
Result result = new Result();
try
{
DemographicFactory.InsertDemographicInfo(d);
result.Status = "Success";
}
catch (Exception ex)
{
result.Status = "Fail";
result.Message = ex.Message;
}
return result;
}