Error parsing JSON from Request: A JSONObject text must begin with '{' at character 0 of

2 posts / 0 new
Last post
derxen
Error parsing JSON from Request: A JSONObject text must begin with '{' at character 0 of

Hi All!

I was thrilled to get started with Mapquest, but I've been stuck for the last hour and I can't seem to figure out why it doesn't work.

I'm using the Route Matrix. The code is as follows:

var currentPos = {latLng: { lat: currentPos.coords.latitude, lng: currentPos.coords.longitude }};

var locations = [ currentPos ];

if( !awty.tripDistance ) {
locations.push( { city: awty.origin } );
}

locations.push( { city: awty.destination} );

let obj = {
locations: locations,
options: {
allToAll: false,
manyToOne: true
}
};

Then I post the object with jQuery.post();

$.post('http://www.mapquestapi.com/directions/v2/routematrix?key=################################', JSON.stringify(obj), function( response ) {
console.log(response);
}, 'json');

Also when I leave out the latLng I still get the same result:

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.

Can anybody tell what I'm doing wrong?

 


derxen
Got it!

Using the contentBody property with value "application/json" was the fix:


$.ajax({
type: 'POST',
url: 'http://open.mapquestapi.com/directions/v2/route?key=################################',
data: JSON.stringify( obj ),
contentType: "application/json",
dataType: 'json'
})
.done(function( response ) {
console.log(response);
});