The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          現(xiàn)在很多廠商比如,百度、谷歌在調(diào)用api接口時早已開始使用json數(shù)據(jù)。
          但是古老的XMLHttpRequest設(shè)計時,為了安全性,不能使用跨站數(shù)據(jù)(就是調(diào)用其它的網(wǎng)站的數(shù)據(jù))
          如果需要訪問由遠程服務(wù)器上一個web服務(wù)托管的json數(shù)據(jù),則要使用JSONP。
          假設(shè),我要進行百度地圖坐標轉(zhuǎn)換,這是文檔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函數(shù)
          通過<script>標簽調(diào)用示例鏈接產(chǎn)生GetValue值時就會彈窗
          然后通過火狐firebug進入控制臺查看GetValue的參數(shù)傳遞給getVal的值
          Object { status=22, message="param error:to illegal, ...support such coord type", result=[0]}
          posted on 2015-01-23 20:50 Eric_jiang 閱讀(468) 評論(2)  編輯  收藏 所屬分類: 微信開發(fā)

          Feedback

          # re: JSONP一種使用元素獲取數(shù)據(jù)的方法 2015-01-23 23:27 `??
          http://www.arceyes.com/down/gisarticle/html/916.html  回復(fù)  更多評論
            

          # re: JSONP一種使用元素獲取數(shù)據(jù)的方法 2015-01-24 00:58 ww

          http://blog.csdn.net/dojotoolkit/article/details/6902981  回復(fù)  更多評論
            

          主站蜘蛛池模板: 华安县| 隆德县| 新疆| 方正县| 清水县| 朔州市| 曲水县| 大新县| 海林市| 宾川县| 西充县| 银川市| 绥化市| 宽城| 太仆寺旗| 雷州市| 武定县| 开远市| 洮南市| 油尖旺区| 隆林| 海原县| 汝城县| 宝丰县| 全州县| 东山县| 镇沅| 淮滨县| 涿州市| 古蔺县| 郎溪县| 巩留县| 禹州市| 侯马市| 松潘县| 靖边县| 扎赉特旗| 巩留县| 麻城市| 拉萨市| 江城|