我有 aspx 页面,我从那里将 jquery ajax 发布请求发送到我的 Web 服务。我已将 Authorization 标头添加到 post call。我的代码是:-
$.ajax({
type: "POST",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + accessToken);
},
url: " My Web Service URL ",
contentType: "application/json; charset=utf-8",
data: { HtmlText: data},
datatype: 'json',
success: function (data) {
alert(JSON.stringify(data));
}
});
在 Web.config 我添加了:-
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Request-Headers" value="Authorization, Content-Type"/>
<add name="Access-Control-Allow-Headers" value="Authorization" />
<add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
</customHeaders>
</httpProtocol>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<directoryBrowse enabled="true" />
发送请求后,我在检查元素(Chrome)中得到响应:-
远程地址:“我的远程地址”请求 URL:“我的主机 URL”请求方法:选项状态代码:405 方法不允许
请求标头查看源代码:-
Accept: / Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8,es;q=0.6,fr;q=0.4,hi;q=0.2 Access-Control-Request-Headers :accept, authorization, content-type Access-Control-Request-Method:POST Connection:keep-alive Host:"My Host URL" Origin:test Referer:/test/MyPage.aspx User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, 像 Gecko) Chrome/35.0.1916.153 Safari/537.36
响应标头查看源代码:-
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-Origin:* Allow:POST缓存控制:无缓存内容长度:76 内容类型:应用程序/json;charset=utf-8 日期:2014 年 7 月 8 日星期二 12:42:10 GMT 过期:-1 Pragma:no-cache Server:Microsoft-IIS/7.5 X-AspNet-Version:4.0.30319