【发布时间】:2012-02-15 13:27:05
【问题描述】:
当我使用 GET 方法时,我有一个 wcf 休息服务,它工作正常(它们与 url 一起传递)。但是我需要使用 post 方法,并且当 post 方法中没有数据传递时,它可以正常工作。但是,当添加一些数据时,它会在 IE 中返回 Bad Request 错误,而在 mozila 和 chrome 中,它只会在 ajax jquery 请求中返回错误。我在这里重新发布了这个问题,因为我无法附加图像..在前面邮政。请帮忙…………
我已经在下面发布了我在 wcf 服务和 ajax 方法中使用的代码。请帮助....
Ischeduler.cs 中的代码
[ServiceContract]
public interface Ischeduler
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetData", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string GetData();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetDataName", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string GetDataName(string Name);
}
scheduler.cs
public string GetData()
{
string getdata = "hello";
return string.Format("You entered" + getdata);
}
public string GetDataName(string Name)
{
return string.Format("You entered" + Name);
}
按钮单击时 Schedular.aspx 中的 Ajax 请求
$("#btnInvoke").click(function () {
$.ajax({
type: "POST",
url: "http://localhost:3125/schedulerAPI_Service/scheduler.svc/GetDataName",
data: '{"Name": "John"}',
dataType: 'json',
contentType: "application/json; charset=utf-8",
crossdomain: true,
success: function (data) {
alert(data);
},
error: function (xhr, status, error) {
alert("failed " + error);
}
});
Wcf 网络配置
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
<connectionStrings>
<add name="InstaScribeCentralConnectionString" connectionString="Data Source=ATLT53;Initial Catalog=InstaScribeCentral;User ID=sa;Password=bmjobmjo;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ServiceBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="scheduler">
<endpoint address="" behaviorConfiguration="ServiceBehavior" binding="webHttpBinding" contract="Ischeduler"/>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
</configuration>
【问题讨论】: