Postcodes.io批量查找

时间:2018-05-21 10:58:32

标签: ajax

我正在尝试使用以下API https://postcodes.io/并使用AJAX执行批量查找。

我可以使用文档中提供的语法进行单个邮政编码查找,如下所示:

RollingRandomAccessFile

然而,“批量查找”的示例没那么有用(看起来它想要一个名为LoggerContext loggerContext = Configurator.initialize("hzServer","log4j2.xml"); 的属性下有数组的对象)我没有设法找到一个工作示例或者自己创建一个。使用上面的示例代码和批量查找的语法我只想执行几个ajax调用来查找大约200个邮政编码(该网站说一次限制为100,所以我可以将它们分成几个数组)。在循环中查找200个邮政编码所花费的时间对我的项目来说并不可行。

1 个答案:

答案 0 :(得分:7)

你需要像下面那样使用它

$.ajax({
        type: "POST",
        url: 'https://api.postcodes.io/postcodes/',
        data: { postcodes: ["code1", "code2"] }
        success: function (response) {
            console.log(response);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            var msg = '';
            if (xhr.status === 0) {
                msg = 'Not connect.\n Verify Network.';
            } else if (xhr.status == 404) {
                msg = 'Requested page not found. [404]';
            } else if (xhr.status == 500) {
                msg = 'Internal Server Error [500].';
            } else if (thrownError === 'parsererror') {
                msg = 'Requested JSON parse failed.';
            } else if (thrownError === 'timeout') {
                msg = 'Time out error.';
            } else if (thrownError === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n' + xhr.responseText;
            }
        }
    });

查看显示预期数据的测试用例

https://github.com/ideal-postcodes/postcodes.io/blob/373fda002692542f21330088154d3d4965a1cd65/tests/filter.integration.js#L33