Ali*_*eit 7 ajax asp.net-ajax httprequest asp.net-web-api2 axios
我在我的 web api 应用程序中启用了 CORS
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
Run Code Online (Sandbox Code Playgroud)
所有的请求都工作正常。但when i pass an object to the post method我明白了:
跨域请求被阻止:同源策略不允许读取位于http://localhost:44367/api/Users/Create的远程资源。(原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。
// WORKING
return axios.post(url)
return axios.get(url)
// NOT WORKING
let model = {
firstName: 'a',
}
return axios.post(url, model);
// or with configuration
let axiosConfig = {
headers: {
'Content-Type': 'application/json;charset=UTF-8',
"Access-Control-Allow-Origin": true,
"Access-Control-Allow-Credentials": true,
}
};
return axios.post(url, model, axiosConfig);
Run Code Online (Sandbox Code Playgroud)
也与邮递员一起发布工作,具有以下正文
//{
// "model" : {
// "name":"firstName"
// }
//}
Run Code Online (Sandbox Code Playgroud)
我在Application_BeginRequest事件中设置了一个断点,它不会命中。
控制器动作
public ClientResponseModel<User> Create([FromBody]UserAddModel model)
{
///.....
}
Run Code Online (Sandbox Code Playgroud)
请求头
Host: localhost:44367
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0
Accept: */*
Accept-Language: en,en-US;q=0.5
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: POST
Access-Control-Request-Headers: access-control-allow-credentials,access-control-allow-origin,content-type
Referer: http://localhost:44346/Registration.aspx
Origin: http://localhost:44346
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
响应头
HTTP/1.1 200 OK
Allow: OPTIONS, TRACE, GET, HEAD, POST
Server: Microsoft-IIS/10.0
Public: OPTIONS, TRACE, GET, HEAD, POST
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcYWxpLmtcc291cmNlXEF6dXJlXExlYmFuZXNlTGF3c1xDTVNcYXBpXFVzZXJzXENyZWF0ZQ==?=
X-Powered-By: ASP.NET
Date: Mon, 24 Feb 2020 13:56:15 GMT
Content-Length: 0
Run Code Online (Sandbox Code Playgroud)
任何帮助真的很感激!
我最终在 my 中添加了以下内容,web.config使其无需在 axios 上进行任何自定义配置即可工作:
<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>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42595 次 |
| 最近记录: |