API 接口 https://lbs.amap.com/api/webservice/guide/api/georegeo
返回数据
{"status":"1","info":"OK","infocode":"10000","count":"1","geocodes":[{"formatted_address":"北京市朝阳区阜通东大街 6 号","country":"中国","province":"北京市","citycode":"010","city":"北京市","district":"朝阳区","township":[],"neighborhood":{"name":[],"type":[]},"building":{"name":[],"type":[]},"adcode":"110105","street":"阜通东大街","number":"6 号","location":"116.482086,39.990496","level":"门牌号"}]}
比如我的 callback=getGPS
$.get('url', getGPS) ???
刚学到回调函数这,回调函数就是写好一个函数,当作参数传给另一个函数调用,那这个 callback 在这个 api 中有什么意义?应该怎么使用?
我现在直接
var results = $.get(url, getGeocodes);
function getGeocodes() {
var tude = results.responseJSON.geocodes[0].location;
$("#id_detect_coordinate").val(tude);
}
}
我没有填那个 callback,直接拿到返回数据, 所以那个 callback 到底有什么用?正确使用方法是什么?
1
maloneleo88 OP 明白了。
``` //回调函数 getGPS <script> function getGPS(data) { //do someting } </script> <script src="请求地址巴拉巴拉&callback=回调函数"> </script> ``` 没加 callback 服务器返回的是 ``` {"status":"1","info":"OK","infocode":"10000","count":"1","geocodes":[{"formatted_address":"北京市朝阳区阜通东大街 6 号","country":"中国","province":"北京市","citycode":"010","city":"北京市","district":"朝阳区","township":[],"neighborhood":{"name":[],"type":[]},"building":{"name":[],"type":[]},"adcode":"110105","street":"阜通东大街","number":"6 号","location":"116.482086,39.990496","level":"门牌号"}]} ``` 加了 callback 以后,服务器返回的是 ``` getGPS({"status":"1","info":"OK","infocode":"10000","count":"1","geocodes":[{"formatted_address":"北京市朝阳区阜通东大街 6 号","country":"中国","province":"北京市","citycode":"010","city":"北京市","district":"朝阳区","township":[],"neighborhood":{"name":[],"type":[]},"building":{"name":[],"type":[]},"adcode":"110105","street":"阜通东大街","number":"6 号","location":"116.482086,39.990496","level":"门牌号"}]}) ``` 也就是说,服务器直接把 回调函数名+(返回数据) 给返回来了。 所以导致 getGPS(data)接收到的实参对象,所以执行 getGPS(data),此时 data 从形参变实参了,在再回调函数中 do something 问题是,用$.get 直接拿数据不香吗? API 这个回调函数的方式除了解决跨域 还有啥用处? |
2
typedefine 2022-05-09 10:25:47 +08:00
JSONP 这么老的方案 居然还有不知道的
|
3
maloneleo88 OP @typedefine 刚学 汗!
|
4
cutemurphy2000 2022-05-12 10:52:31 +08:00
@maloneleo88 哈哈 抱一下·
|