使用 XDomainRequest 从 html 调用 wcf 时出现 400 错误请求
400 Bad Request when calling wcf from html using 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 中的文件,但 none 会提供比迄今为止最好的工具 Fiddler 更多的信息。我还是没有头绪。
显然,问题来自于真实源文件中的拼写错误(此处未出现的拼写错误) 您在上面看到的代码有效。我会在这里留下我的问题,因为它似乎是正确的继续进行的方式,如果它对你不起作用,就像对我一样,很可能是因为打字错误。
我一个多星期以来一直在解决这个问题,但我尝试过的所有方法都没有奏效。我从 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 中的文件,但 none 会提供比迄今为止最好的工具 Fiddler 更多的信息。我还是没有头绪。
显然,问题来自于真实源文件中的拼写错误(此处未出现的拼写错误) 您在上面看到的代码有效。我会在这里留下我的问题,因为它似乎是正确的继续进行的方式,如果它对你不起作用,就像对我一样,很可能是因为打字错误。