2012-06-29 224 views
4

我正在對WCF Web服務發出GET請求。我的WCF服務位於http://localhost/RestService/RestService.svc/web/GetMessage並具有以下接口:jQuery GET失敗,沒有錯誤消息

[OperationContract] 
[WebGet(UriTemplate = "GetMessage", ResponseFormat = WebMessageFormat.Json)] 
String GetMessage(); 

端點被正確配置,因爲我可以讓我的瀏覽器內的裸電話:

<system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="WebServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 

     <endpointBehaviors> 
     <behavior name="WebEndpointBehavior"> 
      <webHttp /> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 

    <services> 
     <service name="TestRestService.RestService" 
       behaviorConfiguration="WebServiceBehavior"> 
     <endpoint name="RestWeb" 
        address="web" 
        binding="webHttpBinding" 
        behaviorConfiguration="WebEndpointBehavior" 
        contract="TestRestService.IRestService" /> 
     </service> 
    </services> 
    </system.serviceModel> 

通過導航在瀏覽器中調用它回覆:

{"GetMessageResult":"Hello World!"} 

到目前爲止好。這裏沒有問題。就讓我們來看看在jQuery documentation用於執行GET產量:

<html> 
    <head> 
     <script type="text/javascript" src="jquery-1.7.2.min.js"></script> 
     <script type="text/javascript"> 
      $.ajax({ 
       url: 'http://localhost/RestService/RestService.svc/web/GetMessage', 
       type: 'GET', 
       dataType: 'json', 
       success: function (data) { 
        alert(data); 
       }, 
       error: function (xhr, status, message) { 
        alert("Error: " + status + " " + message); } 
       }); 
     </script> 
    </head> 
    <body> 
    </body> 
</html> 

我在使用jQuery 1.72小HTML測試頁運行此,我得到以下錯誤:

Error: error 

是怎麼回事?錯誤消息處理程序that I found here給我絕對零有用的信息。簡單地說:

  • 爲什麼我的GET失敗?
  • 爲什麼錯誤信息是無用的?

事實證明,jQuery的本身不支持跨域Ajax請求作爲凱文乙suggested in his answer。爲了解決這個問題,我不得不切換到使用dataType: 'jsonp'並添加webHttpBinding啓用了crossDomainScriptEnabled屬性:

<bindings> 
    <webHttpBinding> 
    <binding name="WebBindingWithScripts" 
      crossDomainScriptAccessEnabled="true"> 
     <security mode="None" /> 
    </binding> 
    </webHttpBinding> 
</bindings> 


<endpoint name="RestWeb" 
      address="web" 
      binding="webHttpBinding" 
      behaviorConfiguration="WebEndpointBEhavior" 
      bindingConfiguration="WebBindingWithScripts" 
      contract="TestService.IRestService"> 
</endpoint> 

當僅使用dataType: 'jsonp',除非您配置WCF服務,允許跨域腳本,你仍然會得到錯誤。

+0

使用try瀏覽器的devtool(用於chrome ctrl + shift + J)。你能看到要求嗎?控制檯上的任何錯誤? – SuperSaiyan

+0

嘗試刪除'dataType'並讓jquery推斷它。你也可以嘗試設置'dataType:'jsonp'。 –

+0

你確定這個web服務正在返回一個MIME類型的json/application嗎? – Ian

回答

2

您是否在提出跨域請求?我看到你正在使用本地主機,但這並不意味着你要求從本地主機(我們知道你可以使用不同的端口或協議)。

有兩方面的原因,該要求會在這種情況下失敗:

  1. 返回的JSON有其他字符,使得它無效JSON
  2. 請求來自不同的域來比web服務。

由於您沒有在控制檯中看到same-origin錯誤,我預計它會成爲#1。但是,由於您得到error而不是parseerror,這不太可能。

+1

我正在本地進行本地請求。頁面和服務位於同一頁面上。事實上,頁面本身只是一個簡單的HTML與我上面發佈的jQuery。 –

+0

更正:我的意思是說,頁面和服務是完全相同的*機器*。然而,我會嘗試把我的測試html頁面放在我的IIS服務器上,看看是否會改變任何東西。 –

+1

這是問題#2。我將我的html文件放在承載我的服務的IIS應用程序上,並且工作正常。如果IIS服務和網頁位於不同的域中,是否有任何解決方法? –

2

這發生在我身上一次,我所做的只是在我的網絡中添加endpointBehaviors。配置來支持這樣的

<behaviors> 
    <serviceBehaviors> 
    ...... 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
     <behavior name="EndpBehavior"> 
      <webHttp/> 
     </behavior> 
    </endpointBehaviors> 

WebHttp請求,並改變了我的端點,包括 「behaviorConfiguration」,改變了結合

<endpoint address="" binding="webHttpBinding" contract="IService1" 
    behaviorConfiguration="EndpBehavior"> 

不要忘記添加到您的IService:

[WebGet(ResponseFormat =WebMessageFormat.Json)] 
+0

我的端點和端點行爲是(不是合同名稱,端點行爲名稱)是相同的。你想讓我將配置添加到問題中嗎? –

+0

是的,邁克會更好。 –

+0

請看我的問題。我已經給了一個SSCE。 –

相關問題