WebApiに関連した多くの質問が呼び出されませんでしたが、すべてのソリューションを試しましたが、私の問題に対して正しい解決策を得ることができません。 私が持っているこのjQueryを使用してコントローラからwebApiを呼び出すことができませんAjax
public class shoppingCart : ApiController
{
[HttpPost]
public string getDetails()
{
return "HttpPost";
}
[HttpGet]
public string getDetails1()
{
return "HttpGet";
}
[HttpPut]
public string getDetails2()
{
return "HttpPut";
}
[HttpDelete]
public string getDetails3()
{
return "HttpDelete";
}
}
私global.asax.csファイルのようなWEBAPIがあるこの
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
私WebApiConfig.csファイルには、この
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
私routeConfig.csのようなものであるようにファイルは次のようなものです
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
私のjQueryのAjaxのスクリプトは、誰かが問題を解決するために私を助けてください私は、Web APIを呼び出そうとするたびに、私はエラー
を取得し、この
$('#btnAjax').click(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: '/api/shoppingCart/getDetails',
success: function (returnData) {
alert('success');
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error');
}
});
});
のようなものです。いくつかの他の回答として
: はコメントで述べたように、あなたもWebApiConfig
public static void Register(HttpConfiguration config)
{
//this line
config.MapHttpAttributeRoutes();
....
}
編集内の次の行を追加する必要があります。この
[HttpPost]
[Route("/api/shoppingCart/getDetails")]
public string getDetails()
{
return "HttpPost";
}
のように試してみてください、あなたに
あなたのコントローラの名前を変更して、コントローラ
のための命名規則は、次のされていません。私はすべての答えを探していて、あなたが提供したリンクの詰め物は、本当に検索友好的ではなかったと私はあなたの与えられたリンクで提案されている答えを読むことができなかった理由だ –