Can't get good response from Route Matrix

8 posts / 0 new
Last post
Can't get good response from Route Matrix

Hi guys,

I'm trying to make a web app using the Route Matrix (directions API). I'm not an experienced coder.

Here is my test page:

<!DOCTYPE html>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>MapQuest test</title>
<script src='jquery.min.js'></script>
$(document).ready(function() {

function testQuery(){
function handleMapQuestResponse(obj) {

var sendText = '{"locations": ["Denver, CO","Westminster, CO","Boulder, CO"],"options": {"allToAll": true}}';

if (confirm('Query MapQuest?')) {
console.log('Trying query...')
$.post(', sendText, handleMapQuestResponse)
<button id="button1">Test Query</button>
<textarea id="test-output"></textarea>

and here's the response from MapQuest:
{"route":{"routeError":{"message":"","errorCode":0}},"info":{"copyright":{"text":"© 2017 MapQuest, Inc.","imageUrl":"","imageAltText":"© 2017 MapQuest, Inc."},"statuscode":400,"messages":["Error processing route request: ","Error parsing JSON from Request: A JSONObject text must begin with '{' at character 0 of , Please see the documentation for the Directions Service at for details on correctly formatting requests."]}}

Looks like I'm not passing the JSON correctly. Please could you let me know how to get a response out of the API?

Many thanks for your time.

Can you see the post contents
Can you see the post contents in the network tab of the developer tools? I wonder if there is some encoding that should or shouldn't be happening.

Yes I can the POST in the

Yes I can the POST in the Network tab (in Firefox), it has the following info:
Status: 200 OK
Method: POST
File: routematrix?key=MYKEY (where MYKEY is my key)
Domain: (unsecure icon)
Cause: (JS icon) xhk
Type: json
Transferred: 500 B
Size: 500 B
(a red/blue bar showing the request took 407 ms)

As of now, the actual response is still as above, i.e. the error saying that a JSONObject must start with {. Strange that I can't execute such a simple test. Is my free account allowed to use Route Matrix? Do I have to enable something for the key to work with this?

FYI I have successfully called Route and Optimized Route via GET, but this is my first try with POST.


Update: I can't POST any data

Update: I can't POST any data to Route either - same error message - seems like the POST data is just not arriving :(

Here's a screenshot showing

Here's a screenshot showing the form data sent to Route, alongside the response (in the textarea):

Make sure to set the json
Make sure to set the json parameter name. Here is how I did it.     <!DOCTYPE html> <html> <head> <title>MapQuest</title> <script src=""></script> <script type="text/javascript"> function init(){ $.post("", "json=" + JSON.stringify({'locations': ['Denver, CO','Westminster, CO','Boulder, CO'],'options':{'allToAll': true}}), function(data) { $("#out").html(data.distance[0][1]); }, "json" ); } </script> </head> <body onload="init();"> <div id="out"></div> </body> </html>

Well that solves it! Thanks

Well that solves it! Thanks so much for the example - I am on my way! :)

Cool! Have fun with it.
Cool! Have fun with it.