带有json参数的Angular HTTP请求 [英] Angular http request with json param

查看:263
本文介绍了带有json参数的Angular HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的RESTful api中,其中一个资源公开了一个GET方法,该方法接受json作为名为"query"的参数.此参数直接传递给MongoDB查询,允许用户使用mongo语法直接查询数据库.

In my RESTful api one of the resources exposes a GET method that accept json as a parameter named 'query'. This parameter is passed directly to the MongoDB query allowing users to query the database directly using mongo syntax.

我遇到的问题是请求始终如下所示:

The problem I'm having is that the request always looks like this:

?&query=%7B%22source%22:%22incident%22%7D 

它应该看起来像这样:

?&query={'source': 'incident'} 

这是我发送GET请求的方式:

This is how I am sending the GET request:

var query = {};
if ($scope.sourceFilter) { query.source = $scope.sourceFilter; }
    var query = JSON.stringify(query);
    $http.get('/api/feedbackEntries', {params: {limit: $scope.limit, query: query}}).success(function(data) { .......

我在其他获取请求上也做同样的事情,但我没有得到这个问题.

I am doing the same thing on other get requests and I don't get this issue.

我在这里做错什么了吗?这与角度解析参数的方式有关吗?

Am I doing something wrong here ? Is this to do with the way angular parses params ?

谢谢

推荐答案

经过一番挖掘,我弄清楚了这一点.看着我提出的要求:

After some digging I figured this one out. Looking at the request I was making:

$http.get('/api/feedbackEntries',

我看到该网址没有以斜杠结尾.与其他可以正常运行的请求相比,这是我唯一能看到的区别.因此,我添加了结尾的斜杠,并且神奇地起作用了.我无法解释为什么,无论是在角度范围内还是其他地方..但这就是我解决问题的方式.

I saw that the url does not end with a trailing slash. This was the only difference I could see compared with other requests that were working fine. So I added the trailing slash and magically it works. I can't explain why, whether it's something within angular or elsewhere .. but this is how I fixed the problem.

希望这对以后的人有帮助.

Hope this helps someone in the future.

这篇关于带有json参数的Angular HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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