jQuery GET失败,没有错误消息

时间:2012-06-29 16:29:53

标签: jquery wcf http-get

我正在针对WCF网络服务发出GET请求。我的WCF服务位于http://localhost/RestService/RestService.svc/web/GetMessage,并具有以下界面:

[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本身并不像Kevin B suggested in his answer那样支持跨域ajax请求。要解决此问题,我必须切换到使用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服务配置为允许跨域脚本,否则仍会出现错误。

2 个答案:

答案 0 :(得分:2)

您是在提出跨域请求吗?我看到你正在使用localhost,但这并不一定意味着你是从localhost请求(因为我们知道你可以使用不同的端口或协议)。

在这种情况下,请求失败有两个原因:

  1. 返回的JSON具有使其无效的其他字符json
  2. 请求来自与Web服务不同的域。
  3. 由于您没有在控制台中看到same-origin错误,我希望它是#1。但是,由于您收到error而非parseerror,因此不太可能。

答案 1 :(得分:2)

这曾经发生在我身上,我所做的就是在我的web.config中添加endpointBehaviors以支持像这样的WebHttp请求

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

并将我的端点更改为包含“behaviorConfiguration”并更改了绑定

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

别忘了将此添加到您的IService:

[WebGet(ResponseFormat =WebMessageFormat.Json)]
相关问题