2013-03-20 65 views
3

我一直在尋找幾個小時,並嘗試不同的事情,讓這個工作。我在stackoverflow上試過這麼多文章,或者我太愚蠢以至於無法正常工作,或者我有一些獨特而奇怪的配置阻止了我體驗快樂。使一個WCF服務接受來自jQuery.AJAX的JSON數據()

創建本教程介紹的WCF服務:

http://www.codeproject.com/Articles/97204/Implementing-a-Basic-Hello-World-WCF-Service

這是超級基本的和有一個方法,所有我希望它做的是讓我與jQuery.AJAX消耗它()使用json。

我有它託管在IIS中,它的工作原理。我可以沒有問題地訪問WSDL。

我嘗試用下面的代碼來使用它:

$.ajax({ 
    dataType: 'json', 
    type: 'POST', 
    contentType: "application/json", 
    url: "//localhost:546/HelloWorldService.svc/GetMessage", 
    data: { 
     name: "Joe" 
    } 
    }).done(function(msg){ 
     console.log(msg); 
     $("#result").append(msg); 
}); 

我總是錯誤。根據我所嘗試的,我得到500個錯誤,402個錯誤,關於錯誤內容的錯誤...所有的錯誤。

我嘗試過從以下文章實施解決方案。它們的範圍從有我改變的web.config端點(我知道我必須要改變他們,但沒有什麼我已經試過到目前爲止工作在加入JSON端點的方面)增加之類的東西

[WebInvoke(Method = "POST", UriTemplate = "json/PostSalesOrderData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] 

的接口。

下面是我看過的一些文章,並試圖粉碎成我的解決方案,使其工作沒有太多成功。

Javascript JSON and WCF webservice on Phonegap Android

HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

WCF Services with JSON, JSONP and SOAP End Points

Two endpoint (soap ,json) and one service method

WCF REST Service not accepting JSON in .Net 4

我也通過本教程中去,並試圖用他說的話讓我的解決辦法工作。依然沒有!

http://www.primordialcode.com/blog/post/passing-json-serialized-objects-wcf-service-jquery

這是我的界面的外觀

[ServiceContract] 
public interface IHelloWorldService 
{ 
    [OperationContract] 
    [WebInvoke(UriTemplate = "GetMessage", Method = "POST", 
     BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json)] 
    String GetMessage(String name); 
} 

誰能幫我體會快樂?

提前致謝,甚至在看我的問題。如果您需要更多信息,或者我沒有提供足夠的信息,請告訴我,以便我可以幫助您!

我一定錯過了一些愚蠢的事......我知道這不是很難。

編輯:

工作網絡。配置

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation debug="false" targetFramework="4.0" /> 
    </system.web> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
    <system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="WebHTTPEndpointBehavior"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="MyServiceTypeBehaviors"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <bindings> 
     <webHttpBinding> 
     <binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/> 
     </webHttpBinding> 
    </bindings> 
    <services> 
     <service name="MyWCFServices.HelloWorldService" 
     behaviorConfiguration="MyServiceTypeBehaviors"> 
     <endpoint address="" binding="webHttpBinding" bindingConfiguration="MyWebServiceBinding" behaviorConfiguration="WebHTTPEndpointBehavior" 
      contract="MyWCFServices.IHelloWorldService"/> 
     <endpoint contract="IMetadataExchange" 
      binding="mexHttpBinding" address="mex"/> 
     </service> 
    </services> 
    </system.serviceModel> 
</configuration> 

回答

4

改變這一行

data: { 
    name: "Joe" 
} 

data: JSON.stringify({name: 'Joe'}); 

編輯:

執行此爲您服務。在配置中添加WebHttp綁定。

<behaviors> 
    <endpointBehaviors> 
    <behavior> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

希望你知道在哪裏添加這個。如果不讓我知道,我會嘗試提供一些輸入。

編輯:

跟進我的評論,

<behavior name="myBehaviorName"> 
     <webHttp /> 
</behavior> 

<service name="MyWCFServices.HelloWorldService" 
     behaviorConfiguration="MyServiceTypeBehaviors"> 
    <endpoint address="" binding="webHttpBinding" 
     contract="MyWCFServices.IHelloWorldService" behaviorConfiguration="myBehaviorName"/> 
    <endpoint contract="IMetadataExchange" 
     binding="mexHttpBinding" address="mex"/> 
    </service> 
+0

感謝您的答覆。我仍然收到500錯誤,雖然...我認爲問題是與服務/ web.config不一定是.ajax調用。 – Steve 2013-03-20 17:38:07

+1

什麼是錯誤描述?和你的網址狀態 「url:」//localhost:546/HelloWorldService.svc/HelloWorldService「但你打的電話是GetMessage?這裏有什麼問題嗎? – 2013-03-20 17:40:58

+0

好的,我根據你的評論取得了進展。將URL改爲Localhost:546/HelloWorldService.svc/GetMessage現在我有這樣的錯誤:POST http:// localhost:546/HelloWorldService.svc/GetMessage 415(無法處理消息,因爲內容類型爲'application/json '不是預期的類型'application/soap + xml; charset = utf-8') – Steve 2013-03-20 17:54:21

1

我的遲到,但你仍然有你需要解決,以得到它的工作的一些問題。您需要更改端點的綁定以支持HTTP操作。

<bindings> 
    <webHttpBinding> 
     <binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/> 
    </webHttpBinding> 
</bindings> 
<behaviors> 
    <endpointBehaviors> 
     <behavior name="WebHTTPEndpointBehavior"> 
      <webHttp helpEnabled="true"/> 
     </behavior> 
    </endpointBehaviors> 
</behaviors> 
<services> 
    <service name="MyWCFServices.HelloWorldService" 
     behaviorConfiguration="MyServiceTypeBehaviors"> 
    <endpoint address="" binding="webHttpBinding" bindingConfiguration="MyWebServiceBinding" behaviorConfiguration="WebHTTPEndpointBehavior" 
     contract="MyWCFServices.IHelloWorldService"/> 
    <endpoint contract="IMetadataExchange" 
     binding="mexHttpBinding" address="mex"/> 
    </service> 
</services> 

maxBufferSizemaxReceivedMessageSize是可選的。

編輯︰哎呀,忘了添加您的behaviorConfiguration英寸

+2

向webHttp行爲添加helpEnabled =「true」並不會造成任何影響。它將允許您查看可用的操作。當您導航到服務綁定點時。 – Justin 2013-03-20 18:43:39

+1

好點。我在我的服務上使用了這個功能,我只是不想增加額外的東西,所以OP可以看到最低要求是什麼。我會更新我的答案。 – Thelonias 2013-03-20 18:52:23

+0

這是一個巨大的幫助。該服務現在至少在工作。該服務告訴我「方法不允許」,但我認爲這超出了我原來的職位範圍。 – Steve 2013-03-20 18:55:46

相關問題