【问题标题】:Why doesn't my WCF JSONP service not return JSONP over HTTPS为什么我的 WCF JSONP 服务不通过 HTTPS 返回 JSONP
【发布时间】:2012-09-26 01:41:55
【问题描述】:

我有一个使用 JSONP(托管在 Azure 中)的 WCF 服务。它在 HTTP 上工作得很好,即,如果它只接收 JSON,它只返回 JSON,如果它接收 JSONP,它返回 JSONP。但是,一旦我切换到 HTTPS(仅在 Azure 中提供 HTTPS 端点),无论调用是 JSON 还是 JSONP,它都只返回 JSON。我对 HTTP 的配置是:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<standardEndpoints>
  <webScriptEndpoint>
    <standardEndpoint name="" crossDomainScriptAccessEnabled="true">          
    </standardEndpoint>
  </webScriptEndpoint>
</standardEndpoints>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

在我的服务 Global.asax 文件中:

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

    void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new ServiceRoute("DirectTvService", new WebScriptServiceHostFactory(), typeof(DirectTVService.DirectTvService)));
    }       
}

我想从HTTP改成HTTPS,所以我加了

<security mode="Transport">
</security>

到standardEndpoint标签,因此:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<standardEndpoints>
  <webScriptEndpoint>
    <standardEndpoint name="" crossDomainScriptAccessEnabled="true">
      <security mode="Transport">
      </security>
    </standardEndpoint>
  </webScriptEndpoint>
</standardEndpoints>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

我将 IIS7 绑定从 HTTP 更改为 HTTPS。使用此配置,服务按预期对 JSON 工作,但仅针对 JSONP 请求返回 JSON(响应未包装在回调函数中。

我的客户请求示例(在 CoffeeScript 中)是:

$.ajax
url: callUrl
dataType: 'jsonp'
data: 
    username: $('#txtUsername').val()
    password: $('#txtPassword').val()
success: (data) =>
    $.unblockUI()
    Application.processLoginData data
    false
error: (d, msg, status) ->
    $.unblockUI()
    alert "There was a problem contacting the database. " + status
    false

我的服务方法装饰有:

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public LoginResponse LoginUser(String username, String password)

有什么想法吗?

库尔特

【问题讨论】:

    标签: ajax wcf azure jsonp


    【解决方案1】:

    奇怪的是,一个可行的解决方案是在 Azure 配置中同时拥有 HTTP 和 HTTPS 端点,但在我的 WCF 配置中只提供一个端点。我使用了以下 serviceModel 配置:

      <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <standardEndpoints>
      <webScriptEndpoint>
        <standardEndpoint name="" crossDomainScriptAccessEnabled="true">
        </standardEndpoint>
      </webScriptEndpoint>
    </standardEndpoints>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    

    一切正常。一旦我删除 Azure 配置中的 HTTP 端点,JSONP 就会停止工作(仅返回 JSON),如果我添加

    <security mode="Transport">
    </security>
    

    到standardEndPoint(正如我期望对HTTPS所做的那样)它也停止工作......

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 2014-08-20
      • 2011-11-18
      • 1970-01-01
      • 2012-06-30
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      相关资源
      最近更新 更多