我正在发送ajax'cors'请求,添加405 Method Not Allowed
标头时得到Authorization
。
我尝试同时发送jquery
和axios
,但仍收到相同的错误响应。
这是我的要求的详细信息:
请求
Request URL:https://localhost:44366/handlers/users.ashx?method=checkifloggedin
Request Method:OPTIONS
Remote Address:127.0.0.1:44366
Status Code:
405
Version:HTTP/2
Referrer Policy:no-referrer-when-downgrade
响应标题:
HTTP/2 405 Method Not Allowed
cache-control: private
allow: GET, HEAD, OPTIONS, TRACE
content-type: text/html; charset=utf-8
server: Microsoft-IIS/10.0
x-sourcefiles: =?UTF-8?B?QzpcVXNlcnNcYWxpLmtcc291cmNlXEF6dXJlXExlYmFuZXNlTGF3c1xDTVNcaGFuZGxlcnNcdXNlcnMuYXNoeA==?=
x-powered-by: ASP.NET
access-control-allow-origin: *
access-control-allow-methods: *
access-control-allow-credentials: True
access-control-allow-headers: *
date: Fri, 31 Jan 2020 10:21:11 GMT
content-length: 5298
X-Firefox-Spdy: h2
请求标题:
Host: localhost:44366
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0
Accept: */*
Accept-Language: en,en-US;q=0.5
Accept-Encoding: gzip, deflate, br
Access-Control-Request-Method: POST
Access-Control-Request-Headers: authorization,content-type
Referer: http://localhost:44351/
Origin: http://localhost:44351
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
TE: Trailers
这是我的axios客户端
const USERS_SERVICE_URI_2 = (method) => `/handlers/users.ashx?method=${method}`;
const axiosClient = axios.create({
baseURL: "https://localhost:44366",
timeout: 5000,
headers: {
'Authorization': getCookie('authToken'),
'Content-Type': 'application/json'
}
});
axiosClient.post(USERS_SERVICE_URI_2('checkifloggedin'))
.then(r => {
console.log(r)
})
.catch(e => {
console.error(e)
})
web.config
<system.webServer>
<handlers>
<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>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Allow-Credentials" value="True" />
<add name="Access-Control-Allow-Headers" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
很抱歉,这个问题很长,但是我需要添加所有详细信息,任何人都可以帮忙吗?
您需要将baseURL
修改为http
,而不是https
会是这样
const axiosClient = axios.create({
baseURL: "http://localhost:44366",
timeout: 5000,
headers: {
'Authorization': getCookie('authToken'),
'Content-Type': 'application/json'
}
});
如果是同样的问题,则可能是WebApi可能阻止了CORS请求。要在WebApi上启用CORS,请使用Microsoft.AspNet.WebApi.Cors
程序包。
您可以阅读有关它here