2012-06-29 178 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。 –

相关问题