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

          web三種跨域請求數據方法

          Posted on 2013-01-11 13:19 penngo 閱讀(9704) 評論(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"}
          主站蜘蛛池模板: 南充市| 乌兰县| 揭西县| 乌鲁木齐市| 巩留县| 玉林市| 密山市| 绥棱县| 钦州市| 麻江县| 湘西| 丰台区| 铜鼓县| 昂仁县| 玉屏| 泸水县| 红原县| 古浪县| 桂平市| 南雄市| 磐安县| 芮城县| 广河县| 开封市| 巫山县| 凤台县| 兰州市| 鹤山市| 梅河口市| 辽阳市| 桂东县| 任丘市| 江西省| 台东市| 白山市| 彭州市| 昭通市| 珲春市| 平和县| 宁都县| 陆河县|