Postcodes.io批量查找

Mr *_*nsk 6 ajax

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

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

$.ajax({
        type: "POST",
        url: 'https://api.postcodes.io/postcodes/BS16AA',
        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;
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

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

Tar*_*ani 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;
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

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

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