我有一个 web 服务,我使用 jquery ajax 调用它来调用它,该服务托管在一个项目中,该项目还在 web.config 中托管 silverlight 项目,我有
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept, X-Requested-With" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
端点配置是
<service behaviorConfiguration="ThreeBirds.HttpServiceBehavior" name="TheWire.Server.Endpoints.LeadCaptureService">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="AspNetAjaxBehavior" name="LeadCaptureService" contract="ThreeBirds.Services.Contracts.ILeadCaptureService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
和自定义行为配置是
<behavior name="AspNetAjaxBehavior">
<enableWebScript />
</behavior>
所以当我从 javascript 调用 web 服务来发布数据时,我得到 '405 method not allowed' 错误
var webMethod = 'RecordSubscribe';
var formFields = {
dealerid: dealerID,
firstname: $(form).find(".inputFirstName").val(),
lastname: $(form).find(".inputLastName").val(),
email: $(form).find(".inputEmail").val()
};
$.ajax({
type: "POST",
url: "http://thewire.3birdsmarketing.com/wire/endpoints/Leadcapture.svc/Record" + webMethod,
processData: false,
contentType: "application/json",
data: JSON.stringify(formFields),
allowCrossDomain: true,
dataType: "json",
success: function (data, status, req) {
alert("Thank you " + formFields.firstname + " " + formFields.lastname + ", your information has been submitted.");
},
error: function (data) {
alert('Sorry ! There was an error sending your data. Please try it again.');
}
});
在 Chrome 中,该服务确实成功,但在 IE (10) 和 firefox (19) 中它失败并且我收到错误消息。Chrome 首先执行 OPTIONS 请求,这会导致 405,就像 IE 和 firefox 中的请求一样,但之后 chrome 会自动执行另一个 POST 请求,并且会出现 200。 IE 和 Firefox 不会执行另一个 POST 请求。
有什么我想念的吗?感谢您的回答