jquery - 在 jquery ajax 调用 wcf 服务上从 http 移动到 https 时出错

标签 jquery asp.net wcf https

我有一个 wcf 服务,托管在 .NET 3.5 Web 应用程序内的 IIS 6.0 中,并且它通过 http 工作正常,但是当我尝试实现 https/ssl 时,我收到 415 错误,并显示以下错误消息。我对请求和响应都使用 json。

“无法处理消息,因为内容类型“application/json”不是预期类型“application/soap+xml;charset=utf-8”。”

下面是我的客户端 jquery.ajax 调用、服务契约(Contract)和 web.config。我在这里缺少什么?

Jquery.ajax 调用:

    $.ajax({
    type: "POST",
        url: "DataShare.svc/GetProgramsByEventType",
        data: '{"eventTypeIds": "' + eventTypeId + '"}',
        contentType: "application/json",
        dataType: "json",
        async: false, //async needs to be false to work with programs dropdown
        success: function (data, status) {
            var programs = data.GetProgramsByEventTypeResult;
            var html = "";
            for (var i = 0; i < programs.length; i++) {
                html += "<li><a href='#'>" + programs[i].m_ProgramLongDesc + "<span class='value'>" + programs[i].m_ProgramID + "</span></a></li>"
            }
            $("#ddlProgramItems").html(html);
        },
        error: function (request, status, error) {
            alert("Error - Status: " + request.status + "\nStatusText: " + request.statusText + "\nResponseText: " + request.responseText);
     }
});

WCF 服务契约(Contract):

<ServiceContract()> _
Public Interface IDataShare
    <OperationContract()> _
    <WebInvoke(Method:="POST",
               BodyStyle:=WebMessageBodyStyle.Wrapped,
               RequestFormat:=WebMessageFormat.Json,
               ResponseFormat:=WebMessageFormat.Json)> _
    Function GetProgramsByEventType(eventTypeIds As String) As List(Of WF.DataContracts.Program.Program)

网络配置:

 <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="EndpointBehavior">
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
    <wsHttpBinding>
      <binding name="TransportSecurity" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        <security mode="Transport">
          <transport clientCredentialType="None"  />
          <!--<message clientCredentialType="Certificate"  negotiateServiceCredential="true"/>-->
        </security>
      </binding>
    </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="ServiceBehavior"
               name="DataShare">
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="IDataShare"
                  bindingConfiguration="TransportSecurity"
                  behaviorConfiguration="EndpointBehavior"/>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

最佳答案

我认为您的 web.config Tone 中需要一个带有 webHttpBinding 的端点。

<endpoint address=""
              binding="webHttpBinding"
              contract="IDataShare"
              bindingConfiguration="TransportSecurity"
              behaviorConfiguration="EndpointBehavior"/>

参见Expose webHttpBinding endpoint in a WCF service

WCF service method to return json or soap

关于jquery - 在 jquery ajax 调用 wcf 服务上从 http 移动到 https 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291960/

相关文章:

jquery-confirm 插件 - 提交表单确认

javascript - jquery中的CSS动画属性

javascript - 实现一个函数输出到另一个函数的逻辑

javascript - 如何使用jQuery从 Angular outerHtml获取所选选项的innerText?

asp.net - 使用 ELMAH 记录数据库错误

asp.net - Angular2 路由不适用于生产 IIS

javascript - 需要将Web Form JavaScript转换为js文件

asp.net - WCF 路由/ESB 架构?

c# - 使用 WCF 和自托管的 Windows 身份验证

java - 如何使用 AXis 1.4 创建 Java 客户端以使用 wsHttpBinding 使用 WCF 服务