在我的 WebApi MVC 4 项目中,脚本中的调用如下:
$(document).ready(function () {
var x = 4;
var y = 0;
iAlertHis('<%=ResolveUrl("~/") %>');
});
.js 文件看起来像:
function iAlertHis(_url) {
$.GetJSON("_url"+"api/History/", function(){});
}
路由如下:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config) {
config.Routes.MapHttpRoute( name: "DefaultApi",
routeTemplate: "api/{controller}/",
defaults: new { id = RouteParameter.Optional }
);
}
}
控制器如下:
public class HistoryController : ApiController
{
public string GetialertHist() {
BALiAlert objBALiAlert = new BALiAlert();
JavaScriptSerializer oSerializer = new JavaScriptSerializer();
string Json = oSerializer.Serialize(objBALiAlert.ialertHist());
return Json;
}
}
运行项目会在调用时出现错误: $.GetJSON(_url+"api/History",function(){});
'Microsoft JScript runtime error: Object doesn't support this property or method'