ASP MVC角JS $ HTTP未找到 [英] ASP MVC Angular JS $http not found
问题描述
我已经从我的角度code调用HTTP GET到的WebAPI控制器出现问题。我使用的ASP MVC只需提供起始页面和起始页面的URL看起来像:的http://本地主机:23845 / StudentsEditor / StudentsView 并从现在的角度,我callinh http请求:
angular.element(文件)。就绪(函数(){
$ HTTP({
方法:GET,
网址:API /组/ GetGroups
数据类型:JSON
})。然后(函数successCallback(响应){
$ scope.groups = response.data;
},功能errorCallback(响应){
警报(出事......);
});
和我收到404因为URL不正确。它的的邻接路径,它像loks:
GET 的http://本地主机:23845 / StudentsEditor / API /组/ GetGroups 的
而不是的http://本地主机:23845 / API /组/ GetGroups
普莱舍给我一些建议锄头解决它。当然,我已经定义RouteConfig:
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);
routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=StudentsEditor,行动=StudentsView,ID = UrlParameter.Optional}
);
和的WebAPI配置:
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
名称:DefaultApi
routeTemplate:{URL} / API / {}控制器/(编号),
默认:新{ID = RouteParameter.Optional}
); VAR JSON = config.Formatters.JsonFormatter;
json.SerializerSettings preserveReferencesHandling = Newtonsoft.Json preserveReferencesHandling.Objects。;
config.Formatters.Remove(config.Formatters.XmlFormatter);
我觉得简单的方式来完成我一直在寻找:
angular.element(文件)。就绪(函数(){
$ HTTP({
方法:GET,
网址:window.location.origin +'/ API /组/ GetGroups',
数据类型:JSON
})。然后(函数successCallback(响应){
$ scope.groups = response.data;
},功能errorCallback(响应){
警报(麻烦..);
});
});
和关键是window.location.origin返回协议+主机+端口
I have a problem with calling http get to WebApi controller from my angular code. I am using ASP MVC just to provide start page and the start page url looks like: http://localhost:23845/StudentsEditor/StudentsView and now from angular I am callinh http request:
angular.element(document).ready(function () {
$http({
method: "GET",
url: "api/Groups/GetGroups",
dataType: "json",
}).then(function successCallback(response) {
$scope.groups = response.data;
}, function errorCallback(response) {
alert("trouble...");
});
and I am getting 404 because the URL is incorrect. It concats the path and it loks like: GET http://localhost:23845/StudentsEditor/api/Groups/GetGroups instead of http://localhost:23845/api/Groups/GetGroups
plese give me some advice hoe to resolve it. Of course I have defined RouteConfig: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "StudentsEditor", action = "StudentsView", id = UrlParameter.Optional }
);
and the webApi config:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{url}/api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
I found easy way to accomplish what I was looking for:
angular.element(document).ready(function () {
$http({
method: "GET",
url: window.location.origin + '/api/Groups/GetGroups',
dataType: "json",
}).then(function successCallback(response) {
$scope.groups = response.data;
}, function errorCallback(response) {
alert("trouble..");
});
});
and the key is window.location.origin which returns protocol + host + port
这篇关于ASP MVC角JS $ HTTP未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!