0

I'm trying to use jquery ajax() to call the mapquest api and I keep getting the following error:

Statuscode:400

"Illegal argument from request: Error parsing JSON from Request: A JSONObject text must begin with '{' at character 0 of , see http://www.mapquestapi.com/geocoding/ for details on correctly formatting locations."

Here is the jquery ajax call I'm making:

$.ajax({
    url: "http://www.mapquestapi.com/geocoding/v1/address?key=<mykey>",
    dataType: 'json',
    type: 'POST',
    contentType:'json',
    data: {json: {location: { "postalCode": "98765"}, options: { thumbMaps: false} } },
    success: function(data) { log( data ) },
    error: function(data) { log( 'error occurred - ' + zipCode + ' - ' + data ) }
});

I've tried jsonp as the dataType as well and I can't get any thing to work.

A url approach works fine but it is more difficult to capture the return response:

http://www.mapquestapi.com/geocoding/v1/address?key=<mykey>&location=89790

Any help would be appreciated.

Matt

khr055
  • 28,690
  • 16
  • 36
  • 48
mafuman42
  • 1
  • 2

2 Answers2

1

It looks like you may have an extra set of brackets when passing in the location info.

Try this:

    $.ajax({
    url: "http://www.mapquestapi.com/geocoding/v1/address?key=",
    dataType: 'json',
    type: 'POST',
    contentType:'json',
    data: {location: { "postalCode": "98765"}, options: { thumbMaps: false} },
    success: function(data) { log( data ) },
    error: function(data) { log( 'error occurred - ' + zipCode + ' - ' + data ) }
});
jharahush
  • 767
  • 4
  • 6
0

It's really because your json isn't really in a json string. You can't just pass a JSONObject into the url like that. You have to stringify it first. I had the same error as you did, and after I stringified it, then it worked. It won't work in IE7, so you have to use JSON2 to make it ie7 compatible: found here: json2

var key = <mykey>;
$.ajax({
    url: "http://www.mapquestapi.com/geocoding/v1/address",
    dataType: 'json',
    type: 'POST',
    contentType:'json',
    data: {
        key: decodeURIComponent(key),
        json : JSON.stringify( 
            {
                location: { "postalCode": "98765"}, options: { thumbMaps: false}
            }) 
    },
    success: function(data) { log( data ) },
    error: function(data) { log( 'error occurred - ' + zipCode + ' - ' + data ) }
});
sksallaj
  • 3,872
  • 3
  • 37
  • 58