简体   繁体   English

AngularJS-发送带有关联数组参数的$ http GET请求

[英]AngularJS - Send $http GET request with associative array params

with this code I am trying to send a $http.get request to my rest service: 与此代码我试图发送$ http.get请求到我的休息服务:

$http({
    method: 'GET',
    url: '/api/item/all',
    params: {
        query: {
            userid: 7
        },
        fields: 'title'
    }
});

I expected to access this URL: 我希望访问此URL:

/api/item/all?query[userid]=7&fields=title

but I am always getting a malformed url... 但我总是收到格式错误的网址...

So, how to proceed? 那么,如何进行呢?

Or even better: Is it possible to pass the full request url into the url-param of $http? 甚至更好:是否可以将完整的请求url传递到$ http的url-param中?

In a GET request, you should pass parameters as part of the query string. 在GET请求中,您应该将参数作为查询字符串的一部分传递。

You can use the shortcut get call and pass in the query parameters in the url: 您可以使用快捷方式get调用并在url中传递查询参数:

$http.get('api/item/all?userid=" + 7 + "&fields=title");

If you need to access them as scope variables, you can do that too: 如果需要将它们作为作用域变量进行访问,也可以执行以下操作:

$http.get('api/item/all?userid=" + $scope.foo + "&fields=" + $scope.bar);

Also depending on the back end you can send them as a path parameters rather then a query parameter: 同样取决于后端,您可以将它们作为路径参数而不是查询参数发送:

$http.get('api/item/all/userid/" + $scope.foo + "/fields/" + $scope.bar);

Hope that helps 希望能有所帮助

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

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