在我的API控制器中称为付款,我有以下方法:
[HttpPost] public HttpResponseMessage Charge(Payment payment) { var processedPayment = _paymentProcessor.Charge(payment); var response = Request.CreateResponse(processedPayment.Status != "PAID" ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK,processedPayment); return response; }
在我的HTML页面中,我有:
$.ajax({ type: "POST",contentType: "application/json; charset=utf-8",url: "http://localhost:65396/api/payment/charge",data: $('#addPayment').serialize(),dataType: "json",success: function (data) { alert(data); } });
每当我开火POST,我得到
"NetworkError: 405 Method Not Allowed - http://localhost:65396/api/payment/charge"
我失踪了什么
谢谢.
UPDATE
以下是路由信息(默认)
routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional } ); routes.MapRoute( name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional } );