我想从客户端使用 AJAX 将 JSON 对象传递给 WCF 服务。在 Internet Explorer 中一切正常,但在 Firefox 中却不行。
在 Firefox 中我得到一个405:Method not allowed
这是我将 json 数据(来自客户端脚本)传递给 WCF 服务的地方......
$(document).ready(function () {
var Author = '{ "Id": "A01", "Name": "Ravinder" }';
$.ajax({
type: "POST",
data: JSON.stringify(Author),
contentType: "application/json; charset=utf-8",
datatype: "json",
url: "http://localhost:53905/Service1.svc/AuthorPostByJson",
success: function (data) {
alert("success");
},
error: function (xmlhttprequest, textstatus, errorthrown) {
alert(" failed ");
console.log("error: " + errorthrown);
}
});//end of $.ajax
});
我的 WCF 服务就像...
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "AuthorPostByJson",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
List<Book> GetBooksByAuthor_JSON(Author author);
我的 web.config 文件....
<system.serviceModel>
<services>
<service behaviorConfiguration="Platform.WebRestful.Service1Behavior"
name="Platform.WebRestful.Service1">
<endpoint address="" binding="basicHttpBinding" contract="Platform.WebRestful.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="Platform.WebRestful.BookServiceHostRestfulBehavior"
name="Platform.WebRestful.BookServiceHostRestful">
<endpoint address="" binding="webHttpBinding" contract="Platform.WebRestful.IBookServiceHostRestful">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Platform.WebRestful.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="Platform.WebRestful.BookServiceHostRestfulBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>