如何对GraphHopper Route Optimization API进行AJAX调用?

时间:2016-11-11 23:22:33

标签: javascript ajax graphhopper

我正在尝试使用GraphHopper路由优化API来解决带有拾取和交付的VRP。我想用https://graphhopper.com/dashboard/#/editor中的一个例子来测试它。我的要求是这样的:

var vrp = {
      "vehicles": [
        {
          "vehicle_id": "my_vehicle",
          "start_address": {
            "location_id": "berlin",
            "lon": 13.406,
            "lat": 52.537
          }
        }
      ],
      "services": [
        {
          "id": "hamburg",
          "name": "visit_hamburg",
          "address": {
            "location_id": "hamburg",
            "lon": 9.999,
            "lat": 53.552
          }
        },
        {
          "id": "munich",
          "name": "visit_munich",
          "address": {
            "location_id": "munich",
            "lon": 11.57,
            "lat": 48.145
          }
        },
        {
          "id": "cologne",
          "name": "visit_cologne",
          "address": {
            "location_id": "cologne",
            "lon": 6.957,
            "lat": 50.936
          }
        },
        {
          "id": "frankfurt",
          "name": "visit_frankfurt",
          "address": {
            "location_id": "frankfurt",
            "lon": 8.67,
            "lat": 50.109
          }
        }
      ]
    };

    $.ajax({
    beforeSend: function(xhrObj){
        xhrObj.setRequestHeader("Content-Type","application/json");
        xhrObj.setRequestHeader("Accept","application/json");
    },
    type: "POST",
    url: 'https://graphhopper.com/api/1/vrp/optimize?key=[...]',       
    data: vrp,               
    dataType: "json",
    success: function(json){
       console.log(json);
    }});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我收到以下回复: screenshot

这可能是问题吗?

1 个答案:

答案 0 :(得分:1)

我已经在另一个问题Send JSON data with jQuery中找到了解决方案。

只需使用data: JSON.stringify(vrp)

即可