AngularJS - 发送$ HTTP GET与关联数组PARAMS要求 [英] AngularJS - Send $http GET request with associative array params

查看:431
本文介绍了AngularJS - 发送$ HTTP GET与关联数组PARAMS要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本code我想一个$ http.get请求发送到我的休息服务:

  $ HTTP({
    方法:GET,
    网址:'/ API /项目/所有',
    params:一个{
        查询:{
            用户名:7
        },
        字段:'标题'
    }
});

我希望访问此网址:

<?pre> / API /项目/所有查询[用户ID] = 7和;田=冠军

但我总是得到一个不正确的网址...

那么,该如何进行呢?

甚至更好?是否可以完整的请求的URL传递到的URL的参数$ HTTP


解决方案

在一个GET请求,你应该通过参数作为查询字符串的一部分。

您可以使用快捷键get调​​用和查询参数中传递的网址:

  $ http.get(API /项目/所有用户id =+ 7 +&放大器;场=标题);

如果您需要访问他们为范围的变量,你可以做到这一点:

  $ http.get(API /项目/所有用户id =+ $ scope.foo +&放大器;场=+ $ scope.bar);

这也取决于后端,你可以把他们作为一个路径参数,而不是一个查询参数:

  $ http.get(API /项目/所有/用户名/+ $ scope.foo +/场/+ $ scope.bar);

希望帮助

with this code I am trying to send a $http.get request to my rest service:

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

I expected to access this 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?

解决方案

In a GET request, you should pass parameters as part of the query string.

You can use the shortcut get call and pass in the query parameters in the 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

这篇关于AngularJS - 发送$ HTTP GET与关联数组PARAMS要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆