posts - 59, comments - 244, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          web三種跨域請求數據方法

          Posted on 2013-01-11 13:19 penngo 閱讀(9692) 評論(0)  編輯  收藏 所屬分類: javascript
          以下測試代碼使用php,瀏覽器測試使用IE9,chrome,firefox,safari

          <!DOCTYPE HTML>
          <html>
          <head>
              <meta charset="UTF-8">
              <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
              <script type="text/javascript" id="loadjson"></script>
              <script type="text/javascript">
              
          // 第一種
              // test1.php在服務器設置請允許跨域(注意:IE9測試不通過)
              $.ajax({
                     type: 'POST',
                     url: 'http:
          //127.0.0.1:8081/test/test1.php',
                     data: 'name=penngo',
                     dataType: 'json',
                     success: 
          function(msg){
                        $('#json').html(JSON.stringify(msg));
                     }
                  });

              
          /* 
              第二種JSONP:
              在客戶端動態注冊一個函數function test(data),然后將函數名傳到服務器,服務器返回一個test({json})到客戶端運行,這樣就調用客戶端的function test(data),從而實現了跨域,jquery已經支持jsonp
              
          */
              
          // test2.php使用jsonp
              $.ajax({
                     type: 'GET',
                     url: 'http:
          //127.0.0.1:8081/test/test2.php?callback=?',
                     data: 'name=penngo',
                     dataType: 'jsonp',
                     success: 
          function(msg){
                        $('#jsonp').html(JSON.stringify(msg));
                     }
                  });

              
          /*
              第三種,原理與jsonp類似,web頁面上調用js文件時不受跨域影響,
              只要利用<script>標簽的src屬性,動態加載js方式就能跨域,該方式為異步,通過testjs()回調
              
          */
              
          var testjs = function(msg){
                   $('#js').html(JSON.stringify(msg));
              }
              $('#loadjson')[
          0].src = 'http://127.0.0.1:8081/test/test3.php?method=testjs&name=penngo';
              </script>

          </head>
          <body>
              header跨域:
              <div id="json">
                  
              </div>
              <br/>
              jsonp跨域:
              <div id="jsonp">
                  
              </div>
              <br/>
              js請求實現跨域:
              <div id="js">
                  
              </div>
          </body>
          </html>

          服務器端處理
          test1.php
          <?php
              header("Access-Control-Allow-Origin: *");
              $name = $_REQUEST['name'];
              $result = array('success'=>1, 'name'=>$name);
              echo json_encode($result);
          ?>

          test2.php
          <?php
              $callback = $_REQUEST['callback'];
              $name = $_REQUEST['name'];
              $result = array('success'=>1, 'name'=>$name);
              $jsonData = json_encode($result);
              echo $callback . "(" . $jsonData . ")";
          ?>

          test3.php
          <?php
              $method = $_REQUEST['method'];
              $name = $_REQUEST['name'];
              $result = array('success'=>1, 'name'=>$name);
              $jsonData = json_encode($result);
              header('Content-type:application/x-javascript');
              echo "$method($jsonData);";
          ?>



          IE9測試,頁面輸出內容
          header跨域: 
          jsonp跨域: 
          {"success":1,"name":"penngo"}
          js請求實現跨域: 
          {"success":1,"name":"penngo"}
          chrome,firefox,safari測試,頁面輸出內容
          header跨域:
          {"success":1,"name":"penngo"}
          jsonp跨域:
          {"success":1,"name":"penngo"}
          js請求實現跨域:
          {"success":1,"name":"penngo"}
          主站蜘蛛池模板: 桃源县| 嫩江县| 桦甸市| 建德市| 达尔| 牙克石市| 新蔡县| 新泰市| 南京市| 阿瓦提县| 三江| 象州县| 石家庄市| 龙州县| 吕梁市| 囊谦县| 阿城市| 开平市| 津南区| 芜湖县| 师宗县| 太原市| 上虞市| 大余县| 沙洋县| 米林县| 定边县| 务川| 濮阳县| 新宁县| 青神县| 安岳县| 梁山县| 松溪县| 梅州市| 丹江口市| 汝州市| 惠安县| 尼勒克县| 剑河县| 辽源市|