2012-09-25 31 views
0

我试图使用jquery的ajax()调用Mapquest服务API和我不断收到以下错误:使用jQuery AJAX调用MapQuest的地理编码API

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

这里jQuery的AJAX调用我做:

$.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) } 
}); 

我已经试过jsonp作为具体的数据类型,以及和我不能得到任何东西 上班。

一个URL方法工作得很好,但它更难以获取返回的响应:

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

任何帮助,将不胜感激。

马特

回答

1

看起来您在传递位置信息时可能会有一组额外的括号。

试试这个:

 
    $.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) } 
}); 
0

这是真的,因为你的JSON是不是真的在一个JSON字符串。你不能只传递一个JSONObject到这样的URL中。你必须先将它串联起来。我有和你一样的错误,并且在我把它串起来之后,它就起作用了。它不会在IE7中工作,所以你必须使用JSON2来使它与ie7兼容:在这里找到: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) } 
}); 
相关问题