jsonp的跨域访问

前端之家收集整理的这篇文章主要介绍了jsonp的跨域访问前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前台代码

<!DOCTYPE html>
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<Meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0">
<Meta name="apple-mobile-web-app-capable" content="yes">
<Meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>上拉下拉动态加载例子</title>
<link href="/css/jquery.mobile.structure-1.3.2.css" rel="stylesheet" type="text/css"/>
<link href="/css/jquery.mobile-1.3.2.css" rel="stylesheet" type="text/css"/>

<script src="/js/jquery.js" type="text/javascript"></script>

<script src="/js/jquery.mobile-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
		   $("#sub").click(function(){
						 $.ajax({
										//url:'<?PHP echo base_url("index.PHP/login/checklogin") ?>',url:"http://localhost/index.PHP/login/checklogin?uname="+$("#uname").val(),type: "get",async: false,//	data:{uname:$("#uname").val()},//	dataType: "text",dataType:"jsonp",jsonp:"callback",jsonpCallback:"test",success: function(json){
											//$("#fanhui").val(data[0].uname);
											alert('您的名字:'+json[0].uname);
										},error: function (jqXHR,textStatus,errorThrown) {
											 alert(textStatus[0]);
									  } 
								 });
			 });
});
</script>
</head>
<body>

<div data-role="page" id="page1">
    <div data-theme="a" data-role="header">
        <h3>
            Header
        </h3>
    </div>
    <div data-role="content">		 
         <div data-role="fieldcontain">
            <input name="uname" id="uname" placeholder="帐号" value="" type="text">
        </div>
        <div data-role="fieldcontain">
            <input name="upass" id="textinput3" placeholder="密码" value="" type="password">
        </div>
		<p id="p1"></p>
		<p id="p2">有用吗??</p>
		
        <input id="sub" type="submit" value="提交" name="sub">
		 <input type="submit" value="取消" name="">
		<h2>返回的内容</h2>
		 <div data-role="fieldcontain">
            <input name="" id="fanhui" placeholder="" value="" type="text">
        </div>
		 <button>向页面发送</button>
    </div>
<div data-role="footer" data-position="fixed">
	<div data-role="navbar" data-grid="a">
			<ul>
				<li><a href="index.html" class="ui-btn-active" data-ajax="false">首页</a></li>
				<li><a href="two.html" data-ajax="false">第二页</a></li>			
			</ul>
		</div>
</div>
</div>
</body>
</html>

CI的Model层:

<?PHP
class Test_m extends CI_Model
{
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	function user_insert($arr)
	{
		$this->db->insert('user',$arr);
	}

	function user_update($id,$arr)
	{
		$this->db->where('uid',$id);
		$this->db->update('user',$arr);
	}

	function user_del($id)
	{
		$this->db->where('uid',$id);
		$this->db->delete('user');
	}

	function user_select($name)
	{
		$this->db->where('uname',$name);
		$this->db->select('*');
		$query=$this->db->get('user');
		return $query->result();
	}

	function user_select_all()
	{
		$this->db->select('*');
		$query=$this->db->get('user');
		return $query->result();
	}

	function user_select_limit($start,$end)
	{
		$this->db->select('*');
		$this->db->limit($end,$start);
		$query=$this->db->get('user');
		return $query->result();
	}
}
?>

CI的Controller层:
<?PHP if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
	function index()
	{
		$this->load->helper('url');
		$this->load->view('jquery');
		
	}
	public function test()
	{
		echo '存在';
	}
	 public function get()
    {
	  $this->load->model("test_model");
	  $user = 
      $data['status'] = 'success'; // 成功的狀態
      $data['result'] = '我要的內容';
      
      echo json_encode($data);
    }
	function checklogin()
	{
		$this->load->model("test_m");
		$user=$this->test_m->user_select($_GET['uname']);       //这边一定要用Get
		if($user)
		{    
			 // echo '成功了';
			// var_dump($user);
			 $test = json_encode($user);
			 $callback = $_GET['callback'];   
			 echo $callback."($test)";
			 exit;
		}
		else
		{
				echo '没有成功,你不是会员';
		}
	}



}
原文链接:/json/290330.html

猜你在找的Json相关文章