Can't get good response from Route Matrix

8 posts / 0 new
Last post
lcphillips
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>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>MapQuest test</title>
<script src='jquery.min.js'></script>
<script>
$(document).ready(function() {
$('#button1').click(testQuery);
});

function testQuery(){
function handleMapQuestResponse(obj) {
console.log(obj);
$("#test-output").val(JSON.stringify(obj));
}

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

if (confirm('Query MapQuest?')) {
console.log('Trying query...')
$.post('http://www.mapquestapi.com/directions/v2/routematrix?key=MYKEY, sendText, handleMapQuestResponse)
}
}
</script>
</head>
<body>
<button id="button1">Test Query</button>
<br/>
<textarea id="test-output"></textarea>
</body>
</html>
-----

and here's the response from MapQuest:
-----
{"route":{"routeError":{"message":"","errorCode":0}},"info":{"copyright":{"text":"© 2017 MapQuest, Inc.","imageUrl":"http://api.mqcdn.com/res/mqlogo.gif","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 http://www.mapquestapi.com/directions/ 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.


MQBrianCoakley
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.

lcphillips
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) www.mapquestapi.com
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.

Cheers


lcphillips
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 :(


lcphillips
Here's a screenshot showing

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

http://imgur.com/a/xqM20


MQBrianCoakley
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="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script type="text/javascript"> function init(){ $.post("http://www.mapquestapi.com/directions/v2/routematrix?key=KEY", "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>

lcphillips
Well that solves it! Thanks

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


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