在使用Web API时,对预检的响应在我的角度项目中具有无效的http状态代码404 [英] Response for preflight has invalid http status code 404 in my angular project while consuming web api
问题描述
我知道这是CORS问题.我已经在Web API服务器端启用了cors. Get方法工作正常,但是在处理post方法时却遇到了问题.请有人用非常简单的示例在Web api和客户端上回答我.关于如何处理印前检查,选项等的说明.
I know that it's CORS problem. I have enabled cors in web api server side. Get method is working fine but while dealing with post method I am facing problem . Please some one answer me with very simple post example both on web api and client side. With explanation of how to deal with preflight, options etc..
控制台
1)zone.js:2935选项 http://localhost:49975/api/Add_Client_/postgoals 404(未找到)
1) zone.js:2935 OPTIONS http://localhost:49975/api/Add_Client_/postgoals 404 (Not Found)
2)无法加载 http://localhost:49975/api/Add_Client_/postgoals :飞行前响应包含无效的HTTP状态代码404.
2) Failed to load http://localhost:49975/api/Add_Client_/postgoals: Response for preflight has invalid HTTP status code 404.
web.config
web.config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Origin, Content-Type, X-Auth-Token"/>
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Content-Type" value="application/json"/>
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
Angular Post方法
Angular Post method
save_Goals(){
let headers : Headers= new Headers();
//headers.append('Content-Type','application/x-www-form-urlencoded');
//headers.append("Access-Control-Allow-Origin","true");
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin','*');
headers.append('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
headers.append('Access-Control-Allow-Headers','Content-Type');
let options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:49975/api/Add_Client_/postgoals', {goal:'foo'},options)
.map(res => res.json());
}
谢谢!
推荐答案
我终于找到了解决方法.我所做的是我从web.config文件中删除了自定义标头.即
I finally found a work around. what i did is i removed custom headers from web.config file. i.e,
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Origin, Content-Type, X-Auth-Token"/>
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Content-Type" value="application/json"/>
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
我已删除
在 WebApiConfig.cs 中,我做了以下更改
var enableCorsAttribute = new EnableCorsAttribute(origins:"*",headers:"*",methods:"*");
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.EnableCors(enableCorsAttribute);
和 Controller 看起来像这样.
[EnableCors(origins: "*", headers: "*", methods: "*", SupportsCredentials = true)]
[RoutePrefix("api/Add_Client_")]
public class Add_Client_Controller : ApiController
{
[AcceptVerbs("POST")]
[HttpPost]
[Route("PostGoals")]
public string PostGoals(string goal)
{
Goal g = new Goal();
g.Goals = goal;
db.Goals.Add(g);
int res = db.SaveChanges();
return ("Success");
}
}
和 Angular POST 方法如下所示
save_Goals(){
let headers : Headers= new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded');
headers.append('Access-Control-Allow-Origin','*');
headers.append('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
headers.append('Access-Control-Allow-Headers','Content-Type');
let options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:49975/api/Add_Client_/PostGoals?goal=check',options)
.map(res => res.json());
}
这可以解决通过URL发送数据的问题.
This is work around to send data with URL.
这篇关于在使用Web API时,对预检的响应在我的角度项目中具有无效的http状态代码404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!