namespace AngularJSDemo.Controllers
{
using Models;
using System.Web.Http;
public class DefaultController : ApiController
{
[HttpPost]
public Person GetPerson([FromBody] Person p)
{
return p;
}
}
}
Here's the module of Person:
namespace AngularJSDemo.Models
{
public class Person
{
public string Name { get; set; }
public string Sex { get; set; }
public string Love { get; set; }
}
}
Here's the Route for the webapi:
namespace AngularJSDemo
{
using Newtonsoft.Json.Serialization;
using System.Net.Http.Formatting;
using System.Web.Http;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Clear();
var jsonFormatInstance = new JsonMediaTypeFormatter();
jsonFormatInstance.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.Add(jsonFormatInstance);
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional,controller="Default" }
);
}
}
}
But when I click the submit button, it always tells me "405" error……Why?
angular.js:10695 POST http://localhost:60031/api 415 (Unsupported Media Type)
Member
184 Points
187 Posts
Why cannot I send a post to my WebApi through AngularJS's POST?
Nov 04, 2016 09:31 AM|852EC327|LINK
Hello all,
I'm using AngularJS and here comes my code:
And here come my WebApi modules:
Here's the module of Person:
Here's the Route for the webapi:
But when I click the submit button, it always tells me "405" error……Why?
angular.js:10695 POST http://localhost:60031/api 415 (Unsupported Media Type)
Member
20 Points
19 Posts
Re: Why cannot I send a post to my WebApi through AngularJS's POST?
Nov 04, 2016 10:57 AM|navakiran|LINK
It looks like an issue with content-type issue in http call. Change the content- type value to 'application/json' .
to
Member
184 Points
187 Posts
Re: Why cannot I send a post to my WebApi through AngularJS's POST?
Nov 05, 2016 12:36 AM|852EC327|LINK
No, it doesn't work:(
All-Star
18815 Points
3831 Posts
Re: Why cannot I send a post to my WebApi through AngularJS's POST?
Nov 07, 2016 05:38 AM|Nan Yu|LINK
Hi 852EC327,
Please try to infer the 'application/json' header:
See more about send objects from AngularJS to WebAPI :
http://www.dotnetcurry.com/aspnet/1049/crud-database-aspnet-webapi-angularjs
http://www.c-sharpcorner.com/uploadfile/cd7c2e/send-object-of-objects-from-angularjs-to-webapi/
Best Regards,
Nan Yu