找不到 ASP MVC Angular JS $http [英] ASP MVC Angular JS $http not found

查看:23
本文介绍了找不到 ASP MVC Angular JS $http的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从我的 Angular 代码调用 http get 到 WebApi 控制器时遇到问题.我使用 ASP MVC 只是为了提供起始页,起始页 url 如下所示:http://localhost:23845/StudentsEditor/StudentsView 现在从 angular 我调用 http 请求:

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...");
        });

我收到 404,因为 URL 不正确.它连接了路径,看起来像:获取 http://localhost:23845/StudentsEditor/api/Groups/GetGroups而不是 http://localhost:23845/api/Groups/GetGroups

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

请给我一些建议来解决它.当然我已经定义了RouteConfig:public static void RegisterRoutes(RouteCollection 路由){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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 }
    );

和 webApi 配置:

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..");
        });
    });

关键是window.location.origin,返回协议+主机+端口

and the key is window.location.origin which returns protocol + host + port

这篇关于找不到 ASP MVC Angular JS $http的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆