1

在 Flask-Restless 文档中,(“进行搜索查询”)我们可以发现,以特殊的 uri 格式查询框架 que 可以查询数据库。

Flask-不安分

http://localhost:5000/api/person?q={"filters":[{"name": "age", "op": "ge", "val": 10}]}
http://localhost:5000/api/person?q={"filters":[{"name": "age", "op": "ge", "val": 10},{"name": "age", "op": "le", "val": 20}]}
...

如何以简单的方式使用 jQuery.ajax 或 XMLHttpRequest 进行 GET 查询?

我尝试过使用 jQuery.param()、encondeURI + JSON.stringify() 进行编码...

抱歉,这一定很明显,但我无法让它工作。

 var url = "http://localhost:5000/api/person?";
 var obj = {"filters":[{"name": "age", "op": "ge", "val": 10}]};
 var jsn = JSON.stringify(obj);
 var encuri = encodeURI(url + "q=" + jsn); //NOPE
 var par = jQuery.param(jsn); //NOPE
 var raw = XMLHttpRequest();raw.open("GET", url + "q=" + encuri);raw.send() //NOPE
 ...

解决方案

$.ajax({
    url: 'http://localhost:5000/api/user',
    data: "q=" + JSON.stringify({"single":true,"filters":[{"name": "id", "op": "eq", "val": 1}]}),
    dataType: 'json',
    contentType: 'application/json; charset=utf-8'
});
4

2 回答 2

2
$.ajax({
    url: 'blah',
    data: JSON.stringify(data),
    dataType: 'json',
    contentType: 'application/json; charset=utf-8'
});

您需要页面中包含 json2.js(和 jquery)。

于 2013-02-25T23:01:04.850 回答
1

首先,我相当肯定这些查询需要转义。

假设 URI 的编码正确(参见 encodeURI 和 encodeURIComponent),您应该能够执行 jQuery.parseJSON。

于 2013-02-25T22:44:58.103 回答