2012-02-15 71 views
0

我有一個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> 

enter image description here

回答

1

首先嚐試在您的WCF服務上啓用Tracing

我已經在你的配置說明幾件事情:

  1. service元素具有這應該是一個完全合格的名稱的名稱屬性值。

  2. 端點元素具有契約屬性值,該值應該是完全限定的名稱。

例:

SampleWCF.Scheduler (i.e. namespace.ServiceName) 

請看看我的樣品後JSON格式數據的WCF服務:

我的WCF服務方法:

[WebInvoke] 
public string GetParam(string Name) 
{ 
    return Name + " from server"; 
} 

現在發佈一些來自提琴手的數據:

enter image description here

當您要傳遞字符串時,用於發佈數據的格式不正確。

+0

請參閱我的更新的答案,如何在REST WCF服務方法中包含字符串參數時發佈數據 – Rajesh 2012-02-15 14:45:04

+0

是啊謝謝你的回答問題是我在請求中傳遞的數據 – deepu 2012-02-17 06:34:35

1

你喜歡IE下添加的屬性做了servrice類

[AspNetCompatibilityRequirements(RequirementsMode 
     = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class Service : IService 
    { 
     // Your code comes here 
    } 

檢查這篇文章是幫助你實現你的任務:Calling WCF Services using jQuery

編輯

檢查這篇文章也可以幫助你完成你的任務:Create REST service with WCF and Consume using jQuery

+0

是的,我已經添加了它 – deepu 2012-02-15 13:35:09

+0

@deepu - 如果可能的話,請遵循artile,下載代碼並檢查你的錯誤 – 2012-02-15 13:39:37

+0

我正在使用休息服務,該服務將是一個http:// localhost:3125/schedulerAPI_Service/scheduler.svc/GetDataName鏈接 – deepu 2012-02-15 13:42:49

0

添加異常處理您的服務看到異常到底是什麼。這樣你不僅可以解決這個異常,而且還可以優雅地處理未來的其他例外情況。

在的Application_Start在你的Global.asax文件如果您尚未創建一個配置添加一行:

//where config is your WebApiConfiguration/HttpConfiguration file 

config.ErrorHandlers = (Handlers, endpoints, descriptions) => 
      { 
       Handlers.Add(new CustomErrorHandler(descriptions)); 
      }; 

,然後創建異常處理程序添加上面的類:

public class CustomErrorHandler : HttpErrorHandler 
{ 
    public IEnumerable<HttpOperationDescription> Descriptions { get; set; } 

    public CustomErrorHandler(IEnumerable<HttpOperationDescription> descriptions) 
    { 
     Descriptions = descriptions; 
    } 

    protected override bool OnTryProvideResponse(Exception exception, ref System.Net.Http.HttpResponseMessage message) 
    { 
     //use exception variable to see what the exception is and then you can either decide 
     //to output a response to the user and do nothing further at which point you return true and set the message variable,  or you can just leave it false to return as is. 

     return false; 
    } 
} 

還等什麼版本你使用的WCF休息現在被稱爲WCF Web API,據我所知它是在0.6,你可以通過NuGet獲得?

相關問題