gpt4 book ai didi

ajax - 跨域请求被阻止 : The Same Origin Policy disallows reading the remote resource (POST request with object)

转载 作者:行者123 更新时间:2023-12-03 15:03:49 25 4
gpt4 key购买 nike

我在我的 web api 应用程序中启用了 CORS

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

所有的请求都工作正常。但是 when i pass an object to the post method我明白了:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:44367/api/Users/Create. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).


// 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);


也与 postman 一起发布工作,具有以下正文
//{
// "model" : {
// "name":"firstName"
// }
//}


我在 Application_BeginRequest 中设置了一个断点事件,它不会命中。

Controller Action
public ClientResponseModel<User> Create([FromBody]UserAddModel model)
{
///.....
}

请求头
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

响应头
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

任何帮助真的很感激!

最佳答案

@Bean
public CorsFilter corsFilter() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
corsConfiguration.setAllowedHeaders(Arrays.asList("Origin", "Access-Control, Allow-Origin", "Content-Type", "Accept", "Authorization", "Origin, Accept", "X-Requested-With", "Access-Control-Request-Method", "Access-Control-Request-Header" )); // this allows all headers
corsConfiguration.setExposedHeaders(Arrays.asList("Origin", "Content-Type", "Accept", "Authorization", "Access-Control-Request-Allow-Origin", "Access-Control-Allow-Credentials"));
corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
return new CorsFilter();
}
我用过这个,对我来说效果很好

关于ajax - 跨域请求被阻止 : The Same Origin Policy disallows reading the remote resource (POST request with object),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60377787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com