传递参数:到Web API可与$ http.get一起使用,而不是$ http.post



angularjs 1.59

此API调用可用于$http.get

JS ViewModel

  $scope.placeOrder = function () { //'api/order/create'
    var order = { AccountId : accountId, Amount : $scope.subTotal,
      Tax: $scope.tax, Shipping: $scope.shipping }
    var orderJSON = JSON.stringify(order);
    viewModelHelper.apiGet('api/order/create', { params: { order: orderJSON } },
      function (result) {
        var orderId = result.data;
      });
  }

app.js

self.apiGet = function (uri, data, success, failure, always) {
  self.isLoading = true;
  self.modelIsValid = true;
  $http.get(AlbumApp.rootPath + uri, data)
      .then(function (result) {
        success(result);
        if (always != null)
          always();
        self.isLoading = false;
      }, function (result) {
        if (failure == null) {
          if (result.status != 400)
            self.modelErrors = [result.status + ': ' + result.statusText +
              ' - ' + result.data];
          else
            self.modelErrors = [result.data + ''];
          self.modelIsValid = false;
        }
        else
          failure(result);
        if (always != null)
          always();
        self.isLoading = false;
      });
}
self.apiPost = function (uri, data, success, failure, always) {
  self.isLoading = true;
  self.modelIsValid = true;
  $http.post(AlbumApp.rootPath + uri, data)
      .then(function (result) {
        success(result);
        if (always != null)
          always();
        self.isLoading = false;
      }, function (result) {
        if (failure == null) {
          if (result.status != 400)
            self.modelErrors = [result.status + ': ' + result.statusText + ' - ' + result.data];
          else self.modelErrors = [result.data];
          self.modelIsValid = false;
        }
        else failure(result);
        if (always != null) always();
        self.isLoading = false;
      });
}

apicontroller

[HttpGet]
[Route("create")]
public IHttpActionResult Create(string order) { 
  var _order = JsonConvert.DeserializeObject<Order>(order); ... }

,但是由于这是一个创建函数,所以我想使用$http.post。当我更改

的电话时
  $scope.placeOrder = function () { //'api/order/create'
    var order = { AccountId : accountId, Amount : $scope.subTotal,
      Tax: $scope.tax, Shipping: $scope.shipping }
    var orderJSON = JSON.stringify(order);
    viewModelHelper.apiPost('api/order/create', { params: { order: orderJSON } },
      //null,
      function (result) {
        var orderId = result.data;
      });
  }

和我的控制器动作

[HttpPost]
[Route("create")] 
public IHttpActionResult Create(string order) { 
  var _order = JsonConvert.DeserializeObject<Order>(order); ... }

我有404错误:

<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:50597/api/order/create'.
</Message>
<MessageDetail>
No action was found on the controller 'OrderApi' that matches the request.
</MessageDetail>
</Error>

这是一个错误还是我错过了一些概念上的点,还是我的代码中有错误?

解决方案:(谢谢Giovani(

params:需要传递给$http.get$http.post中的config。这两种方法具有不同的签名。

apiGet中,将data重命名为config。在apiPost中添加了config

apiPost中添加了null,因此params:传递给config而不是data

app.js

self.apiGet = function (uri, config, success, failure, always) {
  self.isLoading = true;
  self.modelIsValid = true;
  $http.get(AlbumApp.rootPath + uri, config)
...
self.apiPost = function (uri, data, config, success, failure, always) {
  self.isLoading = true;
  self.modelIsValid = true;
  $http.post(AlbumApp.rootPath + uri, data, config)

JS ViewModel

  $scope.placeOrder = function () { //'api/order/create'
    var order = { AccountId : accountId, Amount : $scope.subTotal,
      Tax: $scope.tax, Shipping: $scope.shipping }
    var orderJSON = JSON.stringify(order);
    viewModelHelper.apiPost('api/order/create', null, { params: { order: orderJSON } },
      function (result) {
        var orderId = result.data;
      }); }

$ http.get((和$ http.post((具有不同的方法签名。更多信息

$http.get(<URL>, <DATA (params, responseType, etc..)>)
$http.post(<URL>, <BODY_DATA>, <DATA (params, responseType, etc..)>

最新更新