【问题标题】:Error when moving from http to https on jquery ajax call to wcf service在 jquery ajax 调用 wcf 服务时从 http 移动到 https 时出错
【发布时间】:2012-10-28 19:43:24
【问题描述】:

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

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

下面是我的客户端 jquery.ajax 调用、服务合同和 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 服务合同:

<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)

web.config:

 <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>

【问题讨论】:

    标签: jquery asp.net wcf https


    【解决方案1】:

    我认为您的 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

    【讨论】:

      【解决方案2】:

      请在 Ajax 调用中指定以下内容

      contentType: "application/json; charset=utf-8"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多