如何 AJAX 调用 GraphHopper 矩阵 API

How to make an AJAX call to GraphHopper Matrix API

我需要发送包含以下信息的 json,但在发出请求时我收到错误消息

message: "Unsupported content type application/x-www-form-urlencoded; charset=UTF-8"
status: "finished"

我的请求方式不正确

请求 JS 和 Jquery:

<script>
    $("#request").click(() => {

        var url = "https://graphhopper.com/api/1/matrix?key=d5ab0f9f-538a-4842-926a-9667970d4061";
        var data = {
            "elevation": false,
            "from_points": [-33.467482, -70.624736],
            "to_points": [
                [-33.468756, -70.622155],
                [-33.467359, -70.627332],
                [-33.466348, -70.621985]
            ],
            "from_point_hints": ["nuble"],
            "to_point_hints": [
                "place 1",
                "place 2",
                "place 3"
            ],
            "out_arrays": [
                "distances",
                "times"
            ],
            "vehicle": "car"
        };

        $.ajax({
            url: url,
            data: JSON.stringify(data),
            method: 'POST',
            dataType: 'json',
            success: function(response) {
                console.log(response);
            },
            error: function(error) {
                console.log(error);
            }
        });

    });
</script>

您必须先输入经度,然后输入纬度,您遇到的下一个错误是指定 ajax 请求的头部

代码为:

var url = "https://graphhopper.com/api/1/matrix?key=myKey";
        var data ={
    "from_points": [
        [
           -70.6247406,
           -33.4673461

        ]
    ],
    "to_points": [
        [
            -70.913138,
            -33.794796,
        ]
    ],
    "from_point_hints": [
        "ñuñoa"
    ],
    "to_point_hints": [
        "places 1"
    ],
    "out_arrays": [
        "times",
        "distances"
    ],
    "vehicle": "car"
};

        $.ajax({
            beforeSend: function(xhrObj) {
                xhrObj.setRequestHeader("Content-Type", "application/json");
                xhrObj.setRequestHeader("Accept", "application/json");
            },
            url: url,
            data: JSON.stringify(data),
            type: 'POST', //en este caso
            dataType: 'json',
            success: function(response) {
                console.log(response);
            },
            error: function(error) {
                console.log(error);
            }
        });