【问题标题】:Angularjs Doesn't send parameter value in $http get request to API(Sets to null in API)Angularjs 不向 API 发送 $http 获取请求中的参数值(在 API 中设置为 null)
【发布时间】:2017-01-31 05:24:00
【问题描述】:

我创建了一个下面的工厂,它应该通过将 url 和输入参数作为 No. 来返回一些数据。

(function () {
    'use strict'
    angular.module('MainApp').factory('GetData', ['$http', function ($http) {
        return {
            getCountries: function (url, No) {
                return $http({
                    method: "GET",
                    url: url,
                    DNo: No,
                    
                });
            }
        }

    }]);
})();

I have injected this factory to my controller and used as below.

GetData.getCountries('/General/API/GetDetails', $scope.No).then(function (res) {
                   
                    console.log(res.data);
                }, function (res) {

                });

            }

and here is my API.

 public JsonResult GetDetails(string DNo)
        {
            var allCntry = entity.spGetCountries(DNo).ToList();

            return Json(allCntry, JsonRequestBehavior.AllowGet);
        }

一切都好,工厂调用api但不发送参数值,我的api总是设置/初始化为空值。

【问题讨论】:

    标签: javascript angularjs asp.net-mvc c#-4.0


    【解决方案1】:

    要将搜索参数应用于具有 $http 服务的 URL,请使用配置的 params 属性:

    angular.module('MainApp').factory('GetData', ['$http', function ($http) {
        return {
            getCountries: function (url, No) {
                return $http({
                    method: "GET",
                    url: url,
                    //DNo: No,
                    //USE params property
                    params: { Dno: No }
    
                });
            }
        }
    
    }]);
    

    配置 - params - {Object.<string|Object>} - 将使用 paramSerializer 序列化并作为 GET 参数附加的字符串或对象的映射。

    -- AngularJS $http Service API Reference - Usage.

    【讨论】:

      猜你喜欢
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 2021-01-24
      • 2014-11-26
      • 1970-01-01
      • 2017-12-08
      相关资源
      最近更新 更多