【问题标题】:No HTTP resource was found that matches the request URI web api using angularjs未找到与使用 angularjs 的请求 URI web api 匹配的 HTTP 资源
【发布时间】:2016-04-18 21:42:41
【问题描述】:

我创建了 web api,我正在使用 angularjs 使用它,并且 $http.get 正在工作,但 $http.post 没有显示错误

未找到与请求 URI 匹配的 HTTP 资源

这是我的控制器操作

    [HttpPost]
    public object getItem_byRetailerID(int retailerID, DateTime date)
    {
        string msg = null;
        object listItem = new object();
        try
        {
            mf = new milkFactoryEntities();                                
            listItem = mf.p_item_getItem_byRetailerID(retailerID, date);
        }
        catch (Exception ex)
        {
            msg = "Error : " + ex.Message;
        }
        return Json(new { msg = msg, listItem = listItem });
    }

WebApiConfig

public static void Register(HttpConfiguration config)
    {           

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
            config.Routes.MapHttpRoute(
             name: "api",
            routeTemplate: "login/api/{controller}/{action}"
         );
        config.EnableSystemDiagnosticsTracing();

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

    }

这是我的角度控制器

var param = { retailerID: retailerID, date: $scope.ro.orderDT };
$http.post('http://localhost:60124/api/' + 'retailerOrder/getItem_byRetailerID', JSON.stringify(param)).then(function (d) {        

        console.log(d);
        $scope.listItems = d.data.listItem;

    }, function (error) {
        console.log(error.data);            
    })

请帮忙...

【问题讨论】:

    标签: jquery angularjs asp.net-mvc asp.net-web-api


    【解决方案1】:

    改变你的路线

    routeTemplate: "api/{controller}/{id}" to
    
    routeTemplate: "api/{controller}/{action}/{id}"
    

    并且不需要使用 JSON.stringify(param),它可以是 param

    【讨论】:

    • 网址一定没问题。你的 body 参数构造是否正确?可疑数据参数
      为什么需要代码 config.EnableSystemDiagnosticsTracing(); 下面的行?
    • 我做了一些更改,现在显示错误:XMLHttpRequest 无法加载localhost:60124/api/retailerOrder/getItem_byRetailerID。预检响应包含无效的 HTTP 状态代码 404
    • 去掉config.EnableSystemDiagnosticsTracing();下面的代码并在网络选项卡中的开发人员工具栏中检查正文中的数据。日期值是否格式正确
    • 如何查看数据是怎么回事?
    • 它正在发送 OPTIONS 请求而不是 post
    【解决方案2】:

    对于HTTP POST,从请求正文中读取请求负载。您最多可以有一个查询字符串参数。拥有强类型对象并将其发送到请求负载中通常更容易 -

    self.getData = $http.post('../PostItem_byRetailerID', JSON.stringify(self.data))
    .then(function success(a, b) {
        self.data = a;
    }, function error(a, b) {
        self.data = false;
    });
    

    Test 在哪里 -

    public class Test
    {
        public int retailerID { get; set; }
        public string date { get; set; }
    }
    

    样本数据 -

    {"retailerID": 2 ,"date":"test"}
    

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      相关资源
      最近更新 更多