jquery ajax 跨域

来源:互联网 发布:苏州银行业数据统计 编辑:程序博客网 时间:2024/05/22 06:28

1、jsonp

html

<script type="text/javascript" src="http://www.youxiaju.com/js/jquery-1.4.2.min.js"></script>  <script type="text/javascript">  $(function(){  $.ajax(      {          type:'get',          url : 'http://www.youxiaju.com/validate.php?loginuser=lee&loginpass=123456',          dataType : 'jsonp',          jsonp:"jsoncallback",          success  : function(data) {              alert("用户名:"+ data.user +" 密码:"+ data.pass);          },          error : function() {              alert('fail');          }      }  );  })  </script>  

php

<?php  header('Content-Type:text/html;Charset=utf-8');  $arr = array(      "user" => $_GET['loginuser'],      "pass" => $_GET['loginpass'],      "name" => 'response'  );  echo $_GET['jsoncallback'] . "(".json_encode($arr).")";  

**

2、getJosn

**

  $.getJSON("http://127.0.0.1/server/index.php/jsonp/getJson?callback=?",                { id: "123456", site: "01" },                function(data) {                    console.log(data)                    alert(data.id + ',' + data.site + ',' + data.name);                });

php

$get = $this->input->get();        $id = $get['id'];        $site = $get['site'];        $arr = array(              "id" =>$id,              "site" => $site,              "name" => 'response'          );          echo $_GET['callback'] . "(".json_encode($arr).")"; 

参考:
参考文章

jsonp原理

0 0
原创粉丝点击