【发布时间】:2018-10-05 09:57:57
【问题描述】:
我正在尝试使用 post 方法将 json 数据从 angular 发送到 wcf。 Angular 以 json 格式发送数据,但 wcf 将其作为空对象接收。可以使用get方法发送json。在按钮单击事件上调用 GetData 方法。
Angular 组件.ts
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private httpClient: HttpClient){
}
getData(){
this.httpClient.post('http://localhost:3292/examservice.svc/jsontest',{
RollNumber: '41',
Name: 'Test'
})
.subscribe(
(data:any) => {
console.log(data);
}
)
}
}
WCF 服务.svc.cs
namespace Service
{
public class Service : IService
{
public Student jsont(Student s)
{
return s;
}
}
}
WCF IService.cs
namespace Service
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "jsontest", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
Student jsont(Student s);
}
[DataContract]
public class Student
{
[DataMember]
public string RollNumber { get; set; }
[DataMember]
public string Name { get; set; }
}
}
【问题讨论】: