weixin_33691598 2014-07-23 19:07 采纳率: 0%
浏览 45

AngularJS Put返回404

I'm getting a 404 error with a PUT request when using AngularJS's "$http.put".

Here is my relevant AngularJS code:

    /* toggles whether or not the link is disabled */
    toggleLinkStatus: function (isActive, linkGUID) {
        $http.put('/Explorer/Link/ToggleLinkStatus',
                {

                            isActive: isActive,
                            linkGUID: linkGUID
                }
        );
    }

And my relevant C# Controller Code:

    [HttpPut]
    public IHttpActionResult ToggleLinkStatus(Boolean isActive, Guid linkGUID)
    {
        return Ok();
    }

And my WebApiConfig Code

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        /** define routes for Explorer API */
        config.Routes.MapHttpRoute(
            name: "ExplorerAPI",
            routeTemplate: "Explorer/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

What am I missing here? In the Headers for the call I can see that the data is being passed through in the Request Payload:

isActive: true
linkGUID: "b94badb3-2917-4129-9ae3-35d2a635f66d"

If I do a normal query string PUT using POSTMAN it goes through, but not when I use AngularJS.

UPDATE

Here's something interesting, if I use the following angularJS request it goes through:

    /* toggles whether or not the link is disabled */
    toggleLinkStatus: function (isActive, linkGUID) {
        $http.put('/Explorer/Link/ToggleLinkStatus',
                {
                    isActive: isActive,
                    linkGUID: linkGUID
                },
                {
                    params:
                        {
                            isActive: isActive,
                            linkGUID: linkGUID
                        }
                }
        );
    }

It also works with this:

    /* toggles whether or not the link is disabled */
    toggleLinkStatus: function (isActive, linkGUID) {
        $http.put('/Explorer/Link/ToggleLinkStatus',
                {
                },
                {
                    params:
                        {
                            isActive: isActive,
                            linkGUID: linkGUID
                        }
                }
        );
    }
  • 写回答

2条回答 默认 最新

  • python小菜 2014-07-23 19:34
    关注

    If you're successfully using GET (by doing it in the browser address bar) but not successfully using PUT, then there's something wrong with how you're configuring the route / etc. I don't see what it is in the code snippet you have here, but there's definitely something amiss in how you've configured your routes. Perhaps there's an earlier route that matches the GET that's fouling up. It's not Angular's fault. You can test by using the PostMan plugin for Chrome, it'll let you send PUT/POST/DELETE as well as GET and you can see the results.

    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败