[英]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.