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

时间:2012-11-08 15:21:41

标签: jquery asp.net wcf https

我有一个在.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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

contentType:&#34; application / json;字符集= UTF-8&#34;