description of my issue below.
Situation :
My back-end server is running on WebApi.NET. I have already implemented database model and all of the basic controllers, and they handles GET requests (to download some objects) very well. I've obviously added such entry in Web.config file to allow CORS responds to my AngularJS app, which is running on the other port :
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
</customHeaders>
</httpProtocol>
However I cannot force AngularJS to send new object (that should be placed in database) properly. I've tried to add some headers in AngularJS request too, but no effect.
Here's crucial part of AngularJS app :
self.newRespond = function ()
{
var newRespond = {
'respondID': 3,
'date': ((new Date().getTime()).toString()),
'location': 'PL',
'content': self.respContent,
'author': globalService.nickname,
'imgScr':'Resources/thumb.jpg',
'refID': 1};
$http.post("http://localhost:52095/api/responds", newRespond, {
headers: {
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods' : 'POST, GET, PUT, DELETE',
'Access-Control-Allow-Headers' : 'Content-Type, Authorization, Content-Length, X-Requested-With',
'Content-Type': 'application/x-www-form-urlencoded' }})
.success(function (data)
{
console.log("New respond added!");
})
.error(function (data)
{
console.log("Error while sending respond.")
});
};
And the part of WebApi controller (which does not really matter) :
// POST api/Responds
[ResponseType(typeof(Respond))]
public IHttpActionResult PostRespond(Respond respond)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Responds.Add(respond);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = respond.respondID }, respond);
}
Problem :
After invoking this object-send method, the exception is being thrown :
Since error seems to be connected with OPTIONS header, I've added this :
angularModule.config(['$httpProvider', function ($httpProvider)
{
//Reset headers to avoid OPTIONS request
$httpProvider.defaults.headers.common = {};
$httpProvider.defaults.headers.post = {};
$httpProvider.defaults.headers.put = {};
$httpProvider.defaults.headers.patch = {};
}]);
Sadly, still no results.
Question :
Question is simple - how to fix this issue and force AngularJS to send that damned object to .NET server?
@Edited :
Changed Angular method, as Vu Quyet advised. Still same exception thought :
$http.post("http://localhost:52095/api/responds", newRespond, {
headers: {
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods' : 'GET,POST',
'Access-Control-Allow-Headers' : 'Content-Type',
'Content-Type': 'application/x-www-form-urlencoded' }})
.success(function (data)
{
console.log("New respond added!");
})
.error(function (data, error)
{
console.log("Error while sending respond. " + error)
});
@Edited2 :
Now I have trouble with sending serialized object to WebApi controller. With 'Content-Type' header WebApi controller revives blank object (all fields are equals to null or 0).
$http.post("http://localhost:52095/api/responds", newRespond, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }}) ...
And without this header, controller respond with 415 error - HTTP Error 415 Unsupported media type
.
$http.post("http://localhost:52095/api/responds", newRespond) ...