我有一個wcf rest服務,當我使用GET方法它工作正常(他們與URL一起傳遞)。但我需要使用post方法,並且當沒有數據在post方法中傳遞時,它可以正常工作。但是,當添加一些數據,然後它在IE中返回錯誤請求錯誤,並且在mozila和chrome中,它只是返回ajax jquery請求中的錯誤。由於我無法附加圖像,因此我在此重新發布此問題。帖子。請幫助...........如何使WCF中的POST方法Rest服務在傳遞數據時工作?
我的帖子我在WCF服務和Ajax方法below.pls幫助使用的代碼....
守則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 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>
請參閱我的更新的答案,如何在REST WCF服務方法中包含字符串參數時發佈數據 – Rajesh 2012-02-15 14:45:04
是啊謝謝你的回答問題是我在請求中傳遞的數據 – deepu 2012-02-17 06:34:35