我創建這個AJAX功能:爲什麼AJAX調用不會成功?
function getAssociatedProperties(callback, error) {
$.ajax({
url: '/LayerProperty/get',
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: 5,
crossDomain: true,
success: callback,
error: function() {
}
});
}
這裏是我的web API類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace GeomindMobile.Controllers
{
public class LayerProperty : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public void Post([FromBody]string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
}
每當AJAX功能解僱我得到這個錯誤:
http://localhost/LayerProperty/get 404 (Not Found)
更新
Here is my RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
什麼是錯我的代碼?爲什麼AJAX調用不會成功?
你應該只通過API/LayerProperty/5沒有API/LayerProperty /獲取/ 5 – Milney
'跨域:TRUE'你爲什麼用這個?你需要在同一個域上模擬一個跨域請求嗎?因爲這就是這個設置所做的。 – ADyson
你可以發佈你的路由配置嗎? – juunas