我有一个wcfrest服务,当我使用GET方法时,当我将数据与url一起传递时,它工作正常,但是我需要使用post方法,当没有数据可以在post方法中传递时,它工作正常。 但是当添加一些数据时,它会在 IE 中返回 Bad Request 错误,在 mozila 和 chrome 中它只会返回 Error。我已经发布了我在下面的 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);
}
调度程序.cs
public string GetData()
{
string getdata = "hgello";
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: "GET",
url: "http://localhost:3125/schedulerAPI_Service/scheduler.svc/GetDataName",
data: '{"Name": "John"}',
dataType: 'json',
processdata: true,
contentType: "application/json; charset=utf-8",
crossdomain: true,
success: function (data) {
alert(data);
},
error: function (xhr, status, error) {
alert("failed " + error);
}
});
Wcf Web 配置
<?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>
最佳答案
像这样传递数据。
data: { Name: "John" }
将类型更改为 post
并删除 processdata: true
无论如何,有一个拼写错误,processdata 中的 d
应该是大写 processData
。它的默认值为 true,因此无需指定。
关于jquery - 当数据传入时,WCF Rest Service 中的 POST 方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9138728/