Hi,
Im working on a mapping application using mapquest, and for us to make a call to their server, they recommend the following URL:
(For POST requests)
http://www.mapquestapi.com/directions/v1/route?key=YourKeyGoesHere
For the request, i need 3 parameters:
1. My App Key
2. A location object representing Source
This can be of
type: {street: "SourceCity, SourceState"}
which is a JSON object.
3. A Destination just as above.
For this purpose, i've written the code as:
$('#submit').click(function () {
var appKey = 'MyAppKey';
var source = { street: "Brookings, SD" };
var destination = { street: "Sioux Falls, SD" };
var inputFormat = 'json';
var completeRequest = { key: appKey, locations: [source, destination], inFormat: inputFormat };
$.ajax({
url: 'http://www.mapquestapi.com/directions/v1/route?key=MyAppKey',
type: 'POST',
data: JSON.stringify(completeRequest),
dataType: 'json',
processData: false,
contentType: "application/json; charset=utf-8",
success: function (response) {
$('#feedback').html(response);
},
error: function (response) {
alert("An Error occured when request was made" + response.toString());
}
});
});
The conditions are that, the request needs to have a parameter named
"locations"
which should be formatted as
locations:[{LocationObjSource},{LocationObjDestination}]
.
I am also using
JSON.stringify()
to make request properly formatted as JSON.
All the above code is part of a Razor view in an ASP.NET MVC3 App.
When the request fires, each time, im getting the error function hit. The response is supposed to be JSON. But i don't know what is going wrong.
My assumptions of what might be wrong:
1. Request needs to contain a PARAMETER named
"locations"
which should be of type
locations:[]
as i've said above, so how do i compose this?
Do I use Source and Destination as in my code, which are already JSON objects, or am i doing something wrong.
If anyone as an insight to this, please let me know.
Just for fun, the below URL shows what i'm trying to accomplish.
Have spent many hours on this, any help would be very much appreciated.
Thank you.