Web.config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
</customHeaders>
</httpProtocol>
<handlers>
<remove name="WebDAV"/>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Controller
[EnableCors(origins: "http://domain/api/Clients", headers: "*", methods: "*")]
public class ClientsController : ApiController
{
private ParaNewnergyEntities db = new ParaNewnergyEntities();
// GET: api/Clients
public IEnumerable<Client> GetClient()
{
return db.Client.ToList();
}
[HttpPost]
public IHttpActionResult PostClient(String Username, String Password)
{
Client c = new Client
{
Username = Username,
Password = Password
};
db.Client.Add(c);
db.SaveChanges();
return Ok(c);
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Ajax
var sendData={
Username:"gx",
Password:"gx"
};
$.ajax({
url: "http://domain/api/Clients",
type: "POST",
data: sendData,
success: function (d) {
$("#msg").html(d);
console.log(d);
}
});
Browser Error:
POST http://domain/api/Clients 405 (Method Not Allowed)
I can get API by GET method. It is very weirdo that I can get response from API by POST method without Any Parameter. Otherwise, I just got http error 405.
In addition, I get http error 405 from DELETE and PUT method, too. Only GET and Option return httpcode 200.