2012-02-04 38 views
2

我有,當我使用GET方法,它工作正常,當我通過將數據與URL一起,但我需要使用POST方法,當沒有數據以POST方法傳遞它工作正常.. 一個WCF REST服務但是當添加一些數據時,它會在IE中返回Bad Request錯誤,並且在Mozilla和Chrome中它只是返回Error.I已經發布了我在wcf服務和ajax方法中使用的代碼below.pls幫助POST方法時,數據在它傳遞不工作?

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 = "hgello"; 
     return string.Format("You entered" + getdata); 
    } 
public string GetDataName(string Name) 
    { 
     return string.Format("You entered" + Name); 
    } 
在Schedular.aspx 10

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> 
+0

當我把jQuery.support.cors = TRUE;在的document.ready,我得到了IE的數據,但同樣的錯誤在Mozilla和Chrome – Deepu 2012-02-04 05:55:40

回答

0

傳遞數據。

data: { Name: "John" } 

類型更改爲post和刪除processdata: true反正有一個錯字,d在過程數據應該是資本processData。它的默認值是true,所以不需要指定。

+0

它仍然會返回錯誤的請求 – Deepu 2012-02-04 05:56:18

+0

檢查我的編輯答案。 – ShankarSangoli 2012-02-04 06:06:26

+0

啊我改變了太多,但它仍然給錯誤的請求 – Deepu 2012-02-04 06:51:53

0

您是否嘗試過在你的WCF配置應用enableWebScript像這樣:

<endpointBehaviors> 
    <behavior name="ServiceBehavior"> 
     <enableWebScript/> 
    </behavior> 
</endpointBehaviors> 

除了和@ShankarSangoli一切看起來不錯提到的事情。

+0

我在配置設置後收到此錯誤---使用「UriTemplate」端點不能與「System.ServiceModel.Description.WebScriptEnablingBehavior」使用。 – Deepu 2012-02-04 08:51:07

+0

你有兩個選擇,要麼從'WebInvoke'屬性或http://blogs.msdn.com/b/justinjsmith/archive/2008/02/15/enablewebscript-uritemplate-and-http-刪除'UriTemplate'參數methods.aspx – MonkeyCoder 2012-02-04 09:06:03

0

必須設置aspNetCompatibilityEnabled爲false在web.config中

<serviceHostingEnvironment aspNetCompatibilityEnabled="false"/> 

如果你設置aspNetCompatibilityEnabled爲true,則必須允許的ServiceContract AspNetCompatibilityRequirements模式。

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

您還必須將類型設置爲「POST」。