【问题标题】:Need help getting getJSON to work over SSL需要帮助让 getJSON 通过 SSL 工作
【发布时间】:2012-01-13 00:25:09
【问题描述】:

我有服务

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public Customer GetCustomer(string customerNumber)
{
    return _service.GetCustomer(customerNumber);
}

我使用 jQuery 从网页调用

    var customerNumber = '123456';
    $.getJSON('/JsonServices/B2BServiceJson.svc/GetCustomer', { customerNumber: customerNumber }, customerSelected);

这里是 getJSON 调用的回调:

// HELP! result is null when SSL is enabled
function customerSelected(result) { 
    var customer = result.GetCustomerResult;
    var email = customer.Email;
    // other stuff
}

这是配置

  <system.serviceModel>
    <services>
      <service name="B2B.JsonServices.B2BServiceJson">
        <endpoint address="" binding="webHttpBinding" contract="B2B.JsonServices.B2BServiceJson"
                  behaviorConfiguration="ajaxBehavior" />
      </service>
    </services>


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

<!-- other stuff -->

在我在服务器上启用 SSL 之前,这一切正常。现在 getJSON 调用成功但返回 null。 请帮助我通过 SSL 进行这项工作。谢谢

我的环境是 .Net 3.5、IIS 7

【问题讨论】:

  • 您是否在您的服务上启用了 SSL? stackoverflow.com/questions/425978/…
  • 该服务是为整个站点启用 SSL 的大型网站的一部分。我需要为服务本身做一些明确的事情吗?
  • 对您的配置有疑问?您的配置显示服务类名称和合同名称是相同的“B2B.JsonServices.B2BServiceJson”。这是正确的吗?在没有 SSL 的情况下使用相同的配置之前是否可以正常工作

标签: jquery wcf ssl getjson


【解决方案1】:

这就是我最终的结果,并且效果很好。

  <system.serviceModel>
    <services>
      <service name="B2B_lite.JsonServices.B2BLiteServiceJson">
        <!-- SSL -->
        <endpoint address="" binding="webHttpBinding" contract="B2B.JsonServices.B2BServiceJson"
                  bindingConfiguration="webHttpsBinding" behaviorConfiguration="restBehavior"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpsBinding" closeTimeout="00:00:20">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>

【讨论】:

    【解决方案2】:

    我建议使用:

    <serviceMetadata httpsGetEnabled="true"/>
    

    还有一个 https 绑定:

     <endpoint address="mex" 
               binding="mexHttpsBinding" 
               contract="IMetadataExchange"/>
    

    在您的 serviceModel 配置中。

    <system.serviceModel>
         <bindings>
                <wsHttpBinding>
                        <binding name="wsHttpEndpointBinding">
                                <security mode="Transport">
                                        <transport clientCredentialType ="None"/>
                                </security>
                        </binding>
                </wsHttpBinding>
        </bindings>
        <services>
            <service 
                 behaviorConfiguration="App_WcfWebService.AppWebServiceBehavior"
                 name="App_WcfWebService.AppWebService">
              <endpoint 
                 address="" 
                 binding="wsHttpBinding" 
                 bindingConfiguration ="wsHttpEndpointBinding"
                 contract="App_WcfWebService.IAppWebService">
               </endpoint>
               <endpoint 
                 address="mex" 
                 binding="mexHttpsBinding" 
                 contract="IMetadataExchange"/>
                </service>
        </services>
    
        <behaviors>
                <serviceBehaviors>
                        <behavior name="App_WcfWebService.AppWebServiceBehavior">
                          <serviceMetadata httpsGetEnabled="true"/>
                          <serviceDebug includeExceptionDetailInFaults="true"/>
                <serviceThrottling maxConcurrentSessions="90" />                    
                        </behavior>
                </serviceBehaviors>
        </behaviors>
    

    【讨论】:

    • 谢谢,但这没有用。你还有什么我可以尝试的吗?
    【解决方案3】:

    要做的几件事:

    1. 检查错误,无论是在 web 服务器上,jquery(通过向 Ajax 调用参数列表添加错误函数),或者尝试使用 http 观察程序来查看从服务器返回的响应。
    2. 页面本身是否通过 ssl 访问?
    3. 您在服务器上使用的证书是否被浏览器信任?在 Ajax 调用中,您不能手动接受带有信任警告的证书,因此这可以解释正在发生的事情。

    【讨论】:

    • 服务调用成功(状态 200)但结果为空。该页面正在通过 SSL 调用并且工作正常;只有ajax调用失败。证书来自著名的商业 CA。
    猜你喜欢
    • 2017-09-22
    • 2015-02-11
    • 2011-02-25
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 2016-04-28
    相关资源
    最近更新 更多