zoukankan      html  css  js  c++  java
  • 跨域传递

    //由于浏览器的同源策略原则,所以无法跨域传值,但可以用js原理来实现
    方法一:用jquery中的jsonp
    请求文件:
    <!DOCTYPE>
    <html>
    <head>
    <title>lin3615</title>
    <script src="jquery-1.8.3.min.js" type="text/javascript"></script>
    <script>
    	$(document).ready(function(){
    		$.ajax({
    			type:'get',
    			url:'http://www.example.com/ok.php?loginuser=lin3615&loginpass=123456',
    			dataType:'jsonp',
    			jsonp:'jsonpcallback',
    			jsonpCallback:'yes',
    			success: function(data)
    			{
    				alert("user:"+data.user+",pass:"+data.pass);
    			},
    			error:function(){
    				alert("eeee");
    			}
    		});	
    	});
    </script>
    
    </head>
    <body>
    <h1>xx</h1>
    </body>
    <html>
    // ok.php 文件
    $arr = array(
    							'user'=>$_GET['loginuser'],
    							'pass'=>$_GET['loginpass']
    						);
    echo $_GET['jsonpcallback'].'('.json_encode($arr).')';
    
    //方法二:
    <!DOCTYPE>
    <html>
    <head>
    <title>lin3615</title>
    <script>
    	var flightHandler = function(data)
    	{
    		alert(data.price+","+data.tickets);
    	}
    	var url = "http://www.example.com/ok.php?a=code";
    	var script = document.createElement("script");
    	script.setAttribute('src', url);
    	document.getElementsByTagName('head')[0].appendChild(script);
    </script>
    
    </head>
    <body>
    <h1>xx</h1>
    </body>
    <html>
    // ok.php 文件
    
    if($_GET['a'] == 'codex')
    {
    	echo 'flightHandler({
    		"price":100,
    		"tickets":22
    	})';
    }else
    {
    	echo 'flightHandler({
    		"price":1,
    		"tickets":2
    	})';
    }
    

      

  • 相关阅读:
    服务器出现大量的127.0.0.1:3306 TIME_WAIT连接 解决方法 [转载]
    phpize安装php扩展CURL
    linux位数查看
    Linux下Sublime Text 3的安装
    ECstore后台报表显示空白问题解决办法
    centos 上安装phpstorm
    Nginx禁止目录执行php文件权限
    vue 动画
    vue的路由
    组件的传值 组件之间的通讯
  • 原文地址:https://www.cnblogs.com/lin3615/p/4309986.html
Copyright © 2011-2022 走看看