我已经创建了一个WCF Restfull服务,但当我调用POST
方法时它给了我错误**NetworkError: 400 Bad Request - http://localhost:55827/ProductService.svc/CreateStudent**"
这是我的界面
[ServiceContract]
public interface IProductService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/ProductName/{productID}",BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetProductName(string productID);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "CreateStudent", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)]
bool CreateStudent(Student student);
}
和我的服务类
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class ProductService : IProductService
{
public string GetProductName(string productID)
{
return "ProductName";
}
public bool CreateStudent(Student student)
{
var x = 1;
if (student != null)
return true;
else
return false;
}
}
[DataContract]
public class Student
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public int Age { get; set; }
}
和WCF的web.config是
<system.serviceModel>
<services>
<service behaviorConfiguration="Default"
name="RESTFulWCFService.ProductService">
<endpoint address="" behaviorConfiguration="webBehavior"
binding="webHttpBinding" contract="RESTFulWCFService.IProductService"></endpoint>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
</customHeaders>
</httpProtocol>
<directoryBrowse enabled="true"/>
</system.webServer>
我检查了所有链接,但没有得到任何解决方案
WCF Service returns 400 Bad Request
我的JS代码
var st = {};
st.Name = "A";
st.Address = "B";
st.Age = 8;
$.ajax({
type: "POST",
url: "http://localhost:55827/ProductService.svc/CreateStudent",
data: JSON.stringify(st),
success: function (result) {
alert(result);
console.log(result);
},
error: function (result) {
debugger;
alert('no');
}
});