标签:star -o 类型 art headers htm success error: ajax 跨域
客户端实现:
<?php header("Content-Type:text/html;charset=utf8"); session_start(); $_SESSION["username"] = "骚男"; $_SESSION["userage"] = "25"; $session_name = $_SESSION["username"]; $session_age = $_SESSION["userage"]; ?> <body> <div id="msg"></div> </body> <script src="jquery.js" type="text/javascript"></script> <script> function test() { $.ajax({ //提交数据的类型 POST GET type: "POST", //提交的网址 url: "http://www.test.me/ajax/server.php", //提交的数据 data: { Name: "<?php echo $session_name; ?>", Age: "<?php echo $session_age; ?>", }, //返回数据的格式 datatype: "html", //"xml", "html", "script", "json", "jsonp", "text". //成功返回之后调用的函数 success: function(data) { $("#msg").html(decodeURI(data)); }, //调用出错执行的函数 error: function() { //请求出错处理 alert(‘0‘); } }); } test(); </script>
服务端实现:
<?php header(‘Access-Control-Allow-Origin: *‘); header(‘Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept‘); //主要为跨域CORS配置的两大基本信息,Origin和headers session_start(); $_SESSION["username"] = $_POST[‘Name‘]; var_dump($_SESSION["username"]); ?>
标签:star -o 类型 art headers htm success error: ajax 跨域
原文地址:https://www.cnblogs.com/blts/p/9212577.html