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.

    评论

报告相同问题?