简体   繁体   English

AngularJS如何从需要查询字符串参数和使用角度资源发布数据的URL中获取数据?

[英]AngularJS How do I get data from a URL requiring both querystring parms and post data using angular resource?

Requirement : Send data to an endpoint using a post of data and put the startdate and endate into the querystring of the url, like this: 要求 :使用数据发布将数据发送到端点,并将开始日期和结尾放入URL的查询字符串中,如下所示:

https://server/byLocation?startDate=2019-01-01&EndDate=2020-01-01 HTTPS://服务器/ byLocation的startDate = 2019年1月1日与结束日期= 2020年1月1日?

The data payload only has the locationIDs and Criteria shown below. 数据有效负载仅具有如下所示的locationID和Criteria。

The Resource Definition 资源定义

I've tried moving the startDate and endate out of the query object as well. 我尝试过将startDate和endate也移出查询对象。

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

The EndPoint Definition 端点定义

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

How do I combine querystring in the URL endpoint along with the post data? 如何将URL端点中的querystring与发布数据结合在一起?

The Service: 服务:

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

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


            });


    }

I've tried mixing things up a bit lilke this: 我试着把事情混在一起有点像这样:

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

                locationIds: [locationIds],
                Criteria: Criteria


            });


    }

Am I forced to use $http instead of an endpoint and resource? 我是否被迫使用$ http代替端点和资源?

The browser receives a 400 bad request which looks like this: 浏览器收到一个400错误的请求,如下所示:

Request URL: https://servername/bylocation/?startDate=&endDate= 要求网址: https:// servername / bylocation /?startDate =&endDate =

Clearly the startDate and endDate parms are not being filled in. 显然,startDate和endDate参数没有填写。

The proper way to use AngularJS Endpoints with QueryString and Post data 将AngularJS端点与QueryString和Post数据一起使用的正确方法

This is the proper templating resource pattern: 这是正确的模板资源模式:

ByLocationResource: $resource(

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

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

And this is the call pattern, the first two parms fill in the query string parameters for the endpoint, while the 2nd set of parameters fill in the Post data. 这就是调用模式,前两个参数填充端点的查询字符串参数,而第二组参数填充Post数据。 We named the method query, because we are querying data based on the post parameters, whereby the results are constrained by the Query String by start and end date. 我们将方法命名为query,是因为我们要根据post参数查询数据,因此结果受查询字符串的开始日期和结束日期的限制。

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

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

The code in the MyService service calling the query method. MyService服务中调用查询方法的代码。

function ByLocation(dates, payload) {

    return ByLocationResource.query(dates, payload).$promise;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM