ajax 需要来自瑞典统计局的 json 数据的问题 public API

Problems making ajax call for json data from Statistics Sweden public API

希望在我 ajax 致电瑞典统计局 (SCB) 时获得帮助以识别问题。
SCB API instructions are here

我可以通过以下调用成功获取 table 元数据(说明中的步骤 4.3)

$.ajax({
    url: "http://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0401/BE0401B",
    type: "POST",
    dataType: 'json',
    success: function(response) {
        console.log(response);
    },
    error: function(xhr) {
        console.log(xhr.statusText)
    }
});

但是,当我在下面尝试使用 POST 请求查询 table 时,收到 404 错误未找到。
(示例取自说明中的步骤 4.4 - PDF 第 7 页的下半部分)

或者,如果我尝试使用 GET 请求,我只会得到 table 元数据,但没有结果

$.ajax({
    url: "http://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0401/BE0401B/BefProgFoddaMedel15",
    data: {
        "query": 
           [{"code":"Fodelseland", "selection":{"filter":"item",
           "values":["010","020"]}},
           {"code":"Alder", "selection":{"filter":"all", "values":["*"]}},
           {"code":"Tid", "selection":{ "filter":"top", "values":["3"]}}],
         "response": {"format":"json"}
    },
    type: "POST",  // GET returns only table metadata
    dataType: 'json',
    success: function(response) {
        console.log(response);
    },
    error: function(xhr) {
        console.log(xhr.statusText)
    }
});

编辑
补充说也尝试了 GET 请求,但只有 returns table 元数据而不是统计信息

解决方案
发布前对查询数据调用 JSON.stringify。
Petros Likidis

下面发布的代码

文档似乎有误。如果您 运行 它作为 GET 请求(而不是 POST),它会 returns 结果。

$.ajax({
url: "http://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0401/BE0401B/BefProgFoddaMedel15",
data: {
    "query": 
       [{"code":"Fodelseland", "selection":{"filter":"item",
       "values":["010","020"]}},
       {"code":"Alder", "selection":{"filter":"all", "values":["*"]}},
       {"code":"Tid", "selection":{ "filter":"top", "values":["3"]}}],
     "response": {"format":"json"}
},
type: "GET",
dataType: 'json',
success: function(response) {
    console.log(response);
},
error: function(xhr) {
    console.log(xhr.statusText)
}
});

实际上,API 应该返回“405(方法不允许)”错误,这会让您更好地了解请求失败的原因。资源并没有丢失(正如 404 暗示的那样),只是您必须通过不同的方法访问它。

可能值得向 API 的维护者强调他们的文档是错误的并且他们的 API returns 无用的错误。

我不是javascript方面的专家,但我遇到了同样的问题。我通过调用 JSON.stringify 将 json 查询(post 数据)转换为字符串来解决它,请参阅

http://jsbin.com/punaveteke/edit?js,console,output

$(document).ready(function () {

    $.ajax({
        type: "POST",
        url: "http://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0401/BE0401B/BefProgFoddaMedel15",
        data: JSON.stringify({
            "query": [{
                "code": "Fodelseland",
                "selection": {
                    "filter": "item",
                    "values": ["010", "020"]
                }
            }, {
                "code": "Alder",
                "selection": {
                    "filter": "all",
                    "values": ["*"]
                }
            }, {
                "code": "Tid",
                "selection": {
                    "filter": "top",
                    "values": ["3"]
                }
            }],
            "response": {
                "format": "json"
            }
        }),
        dataType: "json",
        success: function (response) {
            console.log(response);
        },
        error: function (xhr) {
            console.log(xhr.statusText)
        }
    });
});