vb.net XMLHttpRequest 无法加载 [url] 预检响应具有无效的 HTTP 状态代码 400
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32784115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XMLHttpRequest cannot load [url] Response for preflight has invalid HTTP status code 400
提问by Corobori
I am getting an error saying "XMLHttpRequest cannot load [url] Response for preflight has invalid HTTP status code 400.
我收到一条错误消息“XMLHttpRequest 无法加载 [url] 预检响应具有无效的 HTTP 状态代码 400。
I tried calling from a form and it appears to be working. I debug the service from inside Visual Studio and it worked just fine
我尝试从表单调用,它似乎有效。我从 Visual Studio 内部调试服务,它工作得很好
Calling the service this way:
以这种方式调用服务:
$.ajax({
type: "POST",
url: "http://localhost:54664/PopulateCombo.svc/GetCodigo",
data: { EmpresaId: 100100, LanguageId: 5, TipoId: TipoId },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var models = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
for (var i = 0; i < models.length; i++) {
var val = models[i];
var text = models[i];
$('#ddValor').addOption(val, text, false);
}
}
});
My web config.
我的网络配置。
<system.webServer>
....
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Accept, Content-Type, Origin" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
....
</system.webServer>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IPopulateCombo" sendTimeout="00:05:00" />
<binding name="BasicHttpBinding_IPopulateCombo1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:54664/PopulateCombo.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPopulateCombo1"
contract="ACPSvc.IPopulateCombo" name="BasicHttpBinding_IPopulateCombo1" />
</client>
</system.serviceModel>
回答by N0Alias
First, use the JSON.stringify function when passing your data:
首先,在传递数据时使用 JSON.stringify 函数:
data: JSON.stringify({ EmpresaId: 100100, LanguageId: 5, TipoId: TipoId }),
Next, use the webHttp endpoint behavior:
接下来,使用 webHttp 端点行为:
<bindings>
<webHttpBinding>
<binding name="BasicHttpBinding_IPopulateCom" sendTimeout="00:05:00" />
<binding name="BasicHttpBinding_IPopulateCombo1" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="WebHTTPBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<endpoint address="http://localhost:54664/PopulateCombo.svc" binding="webHttpBinding" contract="ACPSvc.IPopulateCombo" behaviorConfiguration="WebHTTPBehavior"/>
</services>
回答by Corobori
I added this in my web.config and it fixed this issue (bumped into another)
我在我的 web.config 中添加了这个,它解决了这个问题(碰到另一个)
<serviceHostingEnvironment>
<serviceActivations>
<add factory="System.ServiceModel.Activation.WebServiceHostFactory"
relativeAddress="./ACPWebSvc/PopulateCombo.svc"
service="PopulateCombo"/>
</serviceActivations>
</serviceHostingEnvironment>

