0

要求:使用 post 数据将数据发送到端点,并将 startdate 和 endate 放入 url 的查询字符串中,如下所示:

https://server/byLocation?startDate=2019-01-01&EndDate=2020-01-01

数据负载仅具有如下所示的 locationID 和 Criteria。

资源定义

我也尝试将 startDate 和 endate 移出查询对象。

ByLocationResource: $resource(
    ByLocationEndpoint,
    null,
    {
        query: {
            startDate: '@startDate',
            endDate: '@endDate',
            locationIds: ['@locationIds'],
            Criteria: '@Criteria',
            method: 'POST'
        }
    }
),

端点定义

var ByLocationEndpoint = https:/servername/byLocation/?startDate=:startDate&endDate=:endDate');

如何将 URL 端点中的查询字符串与发布数据结合起来?

服务:

    function ByLocation(startDate, endDate, Criteria, locationIds) {
        _ByLocationResource.query(
            {

                startDate:startDate,
                endDate:endDate,
                locationIds: [locationIds],
                Criteria: Criteria


            });


    }

我试过把事情混在一起有点像这样:

function ByLocation(startDate, endDate, Criteria, locationIds) {
        _ByLocationResource(startDate,EndDate).query(
            {

                locationIds: [locationIds],
                Criteria: Criteria


            });


    }

我是否被迫使用 $http 而不是端点和资源?

浏览器收到 400 错误请求,如下所示:

请求网址:https://servername/bylocation/?startDate=&endDate=

显然 startDate 和 endDate 参数没有被填写。

4

1 回答 1

0

将 AngularJS 端点与 QueryString 和 Post 数据一起使用的正确方法

这是正确的模板资源模式:

ByLocationResource: $resource(

    ByLocationEndpoint,
    {
        startDate: '@startDate',
        endDate: '@endDate'
    },
    {
        query: {

            Criteria: '@Criteria',
            locationIds: '@locationIds',
            method: 'POST',
            isArray: true
        }
    }
),

这就是调用模式,前两个参数填充端点的查询字符串参数,而第二组参数填充 Post 数据。我们将方法命名为查询,因为我们正在根据 post 参数查询数据,因此结果受开始和结束日期的查询字符串约束。

MyService.ByLocation(
    {
        startDate: startDateTime,
        endDate: endDateTime
    },
    {
        Criteria: {

            Id: Id,
            Minutes: Minutes,
            Id2: Id2
        },
        locationIds: [5, 6, 7, 8]
    }
);

MyService 服务中调用查询方法的代码。

function ByLocation(dates, payload) {

    return ByLocationResource.query(dates, payload).$promise;
}
于 2019-02-22T18:13:25.980 回答