[英]XMLHttpRequest post 400 Bad request error when calling wcf Service
[英]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中的文件,但没有比Fiddler提供更多信息的信息了,到目前为止,Fiddler是最好的工具。 我仍然没有头绪。
显然,问题出在实际源文件中的错字(此处没有出现错字)上面看到的代码有效。 我将在这里留下我的问题,因为似乎这是正确的处理方法,并且如果它对您不起作用,那么它很可能是因为输入错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.