使用XDomainRequest从html调用wcf时出现400 Bad Request

时间:2015-05-06 13:15:19

标签: javascript wcf internet-explorer-9 .net-4.5 xdomainrequest

我已经解决了这个问题超过一周了,而且我所有人都尝试过这样的工作。我从Jquery开始,发现Ie9与其他浏览器不同,并且需要自己的方式来调用wcf。这很简单,我做了所有必需的,但它仍然没有工作! IIS上的这两个地址都是http://localhost。我还重命名了我的html文件,因此它变成了一个aspx文件。我添加了一个Custom ContentTypeMapper,所以我不会与内容类型发生冲突。我不会添加我在这里尝试的所有内容,因为我可以用它来写一本书。你到目前为止我找到了最好的东西。我收到了来自Fiddler的错误消息。这是代码:

使用Javascript:

    function CallService() {
        var xdr;

        function err() {
            alert('Error');
        }
        function timeo() {
            alert('Time off');
        }
        function loadd() {
            alert('Response: ' +xdr.responseText);
        }
        function stopdata() {
            xdr.abort();
        }   
        xdr = new XDomainRequest();
        if (xdr) {
            xdr.onerror = err;
            xdr.ontimeout = timeo;
            xdr.onload = loadd;
            xdr.timeout = 10000;
            xdr.open('POST','http://localhost/Path/Service.svc/AjaxEndpoint' + '/FunctionName');
            xdr.send()
        } else {
            alert('XDR undefined');
        }             
    }

服务(vb.net)

<ServiceContract()>
Public Interface IService

    <OperationContract()> _
    <WebInvoke(Method:="POST")> _
    Function FunctionName() As String

End Interface

Public Class Service
    Implements IService

    Public Function FunctionName() As String Implements IService.FunctionName
        Return "test"
    End Function
End Class

Public Class CustomContentTypeMapper
    Inherits WebContentTypeMapper
    Public Overrides Function GetMessageFormatForContentType(contentType As String) As WebContentFormat
        If contentType = "application/octet-stream" Then
            Return WebContentFormat.Json
        Else
            Return WebContentFormat.Default
        End If
    End Function
End Class

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="AppName.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <!--Generate via 16 caractères https://www.random.org/strings/ -->
    <add key="envRnd" value="w6pgpNTDke3fpAd1" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="CustomMapper">
          <webMessageEncoding webContentTypeMapperType="AppName.CustomContentTypeMapper, AppName" />
          <httpTransport manualAddressing="true" />
        </binding>
      </customBinding>
      <webHttpBinding>
        <binding name="AjaxBinding"/>
      </webHttpBinding>
    </bindings>
    <client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
          <behavior name="ServiceBehaviors">
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
          </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
      <service name="AppName.Service" behaviorConfiguration="ServiceBehaviors" >
        <endpoint address="AjaxEndpoint" behaviorConfiguration="AjaxBehavior" binding="customBinding"
            contract="AppName.IService" bindingConfiguration="CustomMapper"/>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <!--<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
      </customHeaders>
    </httpProtocol>-->
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

  <location path="Service.svc">
    <system.web>
      <authorization>
        <allow roles="prd" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <applicationSettings>    
  </applicationSettings>
</configuration>

编辑: 我查看了httperr1.log和inetpub \ logs \ LogFiles \ W3SVC1中的文件,但是到目前为止,没有人能比Fiddler提供更多的信息。我仍然没有线索。

1 个答案:

答案 0 :(得分:0)

显然,问题来自真实源文件的拼写错误(这里没有出现的拼写错误)上面看到的代码有效。我会在这里留下我的问题,因为它似乎是正确的方式继续下去,如果它对你不起作用,就像它对我一样,很可能是因为一个错字。