【发布时间】:2014-07-17 17:56:09
【问题描述】:
我创建了一个由 jquery 使用的 WCF 休息服务。该服务托管在我的本地计算机中。该服务似乎仅在我使用 GET 动词时才有效。如果我使用任何其他我得到 405(不允许方法)错误。我想使用 jquery 向我的服务发布一个 json 对象。
[ServiceContract]
public interface ITest
{
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate="Contact", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[Description("Send contact us message.")]
string Log(Someobject object);
}
客户代码:
$.ajax({
type: "PUT",
url: "http://localhost/servicename/ContactUs.svc/Contact",
data: JSON.Stringify(jsonObject),
contentType: "application/json; charset=utf-8",
processData:true,
dataType: "jsonp",
success: function (data) {
alert("Ping" + data);
},
error: function (error) {
alert("Failed to ping server" + error);
}
});
网页配置
<behavior name="ContactUsBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ContactUsEndPointBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<services>
<service name="servicename.ContactUs" behaviorConfiguration="ContactUsBehaviour">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="JSONPBinding" contract="servicename.IContactUs" behaviorConfiguration="ContactUsEndPointBehavior"/>
</service>
</services>
【问题讨论】:
标签: jquery json wcf rest wcf-rest