The NoteBook of EricKong

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
现在很多厂商比如,百度、谷歌在调用api接口时早已开始使用json数据。
但是古老的XMLHttpRequest设计时,为了安全性,不能使用跨站数据(就是调用其它的网站的数据)
如果需要访问由远程服务器上一个web服务托管的json数据,则要使用JSONP。
假设,我要进行百度地图坐标转换,这是文档http://developer.baidu.com/map/changeposition.htm
运行示例 http://api.map.baidu.com/geoconv/v1/?coords=114.21892734521,29.575429778924;114.21892734521,29.575429778924&from=1&to=1
浏览器打开得到返回值 (注意当前返回错误,本文只是随便找个例子用来讲解)
{"status":22,"message":"param error:to illegal, not support such coord type","result":[]}
那么我们该如何来让我们的页面程序获取这个值呢?
在示例链接中加入
callback=GetValue
然后新建html页面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script>
        var getVal;
        function GetValue(Value)
        {
            getVal = Value;
            alert("ok");
        }
        
    </script>
</head>
<body>
    <script src="http://api.map.baidu.com/geoconv/v1/?coords=114.21892734521,29.575429778924;114.21892734521,29.575429778924&from=1&to=1&callback=GetValue">
    </script>
</body>
</html>
!注意html页面中新建的GetValue函数
通过<script>标签调用示例链接产生GetValue值时就会弹窗
然后通过火狐firebug进入控制台查看GetValue的参数传递给getVal的值
Object { status=22, message="param error:to illegal, ...support such coord type", result=[0]}
posted on 2015-01-23 20:50 Eric_jiang 阅读(417) 评论(2)  编辑  收藏 所属分类: 微信开发

Feedback

# re: JSONP一种使用元素获取数据的方法 2015-01-23 23:27 `😃
http://www.arceyes.com/down/gisarticle/html/916.html  回复  更多评论
  

# re: JSONP一种使用元素获取数据的方法 2015-01-24 00:58 ww

http://blog.csdn.net/dojotoolkit/article/details/6902981  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: