AngularJS POST Fails: Response for preflight has invalid HTTP status code 404
EDIT:
It's been years, but I feel obliged to comment on this further. Now I actually am a developer. Requests to your back-end are usually authenticated with a token which your frameworks will pick up and handle; and this is what was missing. I'm actually not sure how this solution worked at all.
ORIGINAL:
Ok so here's how I figured this out.
It all has to do with CORS policy. Before the POST request, Chrome was doing a preflight OPTIONS request, which should be handled and acknowledged by the server prior to the actual request. Now this is really not what I wanted for such a simple server. Hence, resetting the headers client side prevents the preflight:
app.config(function ($httpProvider) {
$httpProvider.defaults.headers.common = {};
$httpProvider.defaults.headers.post = {};
$httpProvider.defaults.headers.put = {};
$httpProvider.defaults.headers.patch = {};
});
The browser will now send a POST directly. Hope this helps a lot of folks out there... My real problem was not understanding CORS enough.
Link to a great explanation: http://www.html5rocks.com/en/tutorials/cors/
Kudos to this answer for showing me the way.
Response for preflight has invalid http status code 404 in my angular project while consuming web api
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>
This content i removed
and in WebApiConfig.cs i did following changes
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);
and Controller Looks like this.
[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");
}
}
and Angular POST Method looks like following
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());
}
This is work around to send data with URL.
Related Topics
What Are Data-* HTML Attributes
Difference Between Node Object and Element Object
Disable Interpolation When Scaling a ≪Canvas≫
Which Browsers Support ≪Script Async="Async" /≫
How to Parse an Rss Feed Using JavaScript
Pass a String Parameter in an Onclick Function
Angularjs - Value Attribute on an Input Text Box Is Ignored When There Is a Ng-Model Used
Performance of Mutationobserver to Detect Nodes in Entire Dom
How to Prevent a Click on a '#' Link from Jumping to Top of Page
Set Custom Html5 Required Field Validation Message
How to Calculate the Xpath Position of an Element Using JavaScript
How to Pass Parameters on Onchange of HTML Select
Including Both Href and Onclick to HTML ≪A≫ Tag
Restricting Input to Textbox: Allowing Only Numbers and Decimal Point
Video Auto Play Is Not Working in Safari and Chrome Desktop Browser
Stop/Close Webcam Stream Which Is Opened by Navigator.Mediadevices.Getusermedia