
批改狀態(tài):合格
老師批語:是不是覺得跨域請求非常的簡單呢
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>cors跨域請求</title>
</head>
<body>
<h2>CORS跨域請求</h2>
<div>
<label for="stuName">姓名:</label>
<span id="stuName"></span>
</div>
<div>
<label for="stuAge">年齡:</label>
<span id="stuAge"></span>
</div>
<div>
<button onclick="getData()">通過CORS獲取其它域數(shù)據(jù)</button>
</div>
<script>
function getData() {
var xml = new XMLHttpRequest();
xml.onreadystatechange = function () {
if (xml.readyState === 4 && xml.status === 200) {
$data = JSON.parse(xml.responseText);
console.log($data.name, $data.age);
document.getElementById("stuName").innerHTML = $data.name;
document.getElementById("stuAge").innerHTML = $data.age;
}
};
//當(dāng)前地址
//http://php11.edu/0522/cors.html
//跨域請求數(shù)據(jù),需要在data.php文件中設(shè)置請求頭
xml.open("GET", "http://58city.com/data.php", true);
//http://58city.com/data.php
//data.php中設(shè)置請求頭
//如果使用post請求,需要設(shè)置下面一條
// header("Content-Type:text/plain");
//設(shè)置頭部,不設(shè)置的話請求會被拒絕
// header("Access-Control-Allow-Origin:http://php11.edu");
xml.send(null);
}
</script>
</body>
</html>
<?php
//http://58city.com/data.php
//如果使用post請求,需要設(shè)置下面一條
header("Content-Type:text/plain");
//設(shè)置頭部,不設(shè)置的話請求會被拒絕
header("Access-Control-Allow-Origin:http://php11.edu");
$stu['name'] = 'angle';
$stu['age'] =32;
echo json_encode($stu);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>jsonP實現(xiàn)跨域請求</title>
</head>
<body>
<h2>jsonP實現(xiàn)跨域請求</h2>
<div>
<label for="stuName">姓名:</label>
<span id="stuName"></span>
</div>
<div>
<label for="stuAge">年齡:</label>
<span id="stuAge"></span>
</div>
<div>
<button onclick="getData()">通過jsopP獲取其它域數(shù)據(jù)</button>
</div>
<script>
//jsonP回調(diào)函數(shù)
//handle中的參數(shù)是一個對象,(參數(shù)已經(jīng)解析json字符串為一個對象)
function handle(jsonPData) {
var $data = jsonPData;
console.log($data.name, $data.age);
document.getElementById("stuName").innerHTML = $data.name;
document.getElementById("stuAge").innerHTML = $data.age;
}
//html頭部動態(tài)創(chuàng)建一個script標(biāo)簽,通過src屬性獲取外部數(shù)據(jù)
//scr = "http://58city.com/data2.php?jsonp=回調(diào)函數(shù)名";
function getData() {
var script = document.createElement("script");
script.src = "http://58city.com/data2.php?jsonp=handle";
document.head.appendChild(script);
//http://58city.com/data2.php
//data2中的數(shù)據(jù)
// <?php
// $stu['name'] = 'Eric';
// $stu['age'] =6;
// $jsonStr = json_encode($stu);
// //handle是請求數(shù)據(jù)的回調(diào)函數(shù)名,參數(shù)是一個json字符串
// echo "handle($jsonStr)";
// ?>
}
</script>
</body>
</html>
<?php
//http://58city.com/data2.php
$stu['name'] = 'Eric';
$stu['age'] =6;
$jsonStr = json_encode($stu);
//handle是請求數(shù)據(jù)的回調(diào)函數(shù)名,參數(shù)是一個json字符串
echo "handle($jsonStr)";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>商品展示頁</title>
<link rel="stylesheet" href="showGoods.css" />
</head>
<body>
<div class="show" id="show">
<h2>商品展示頁面</h2>
<div>
<span>編號</span><span>名稱</span><span>價格</span><span>單位</span
><span>時間</span>
</div>
</div>
</body>
<script>
//jsonP回調(diào)函數(shù)
//handle中的參數(shù)是一個對象,(參數(shù)已經(jīng)解析json字符串為一個對象)
function handle(jsonPData) {
// //創(chuàng)建一個div標(biāo)簽
// var div = document.createElement("div");
// //添加到html中
// document.getElementById("show").appendChild(div);
// //設(shè)置div的id值
// div.id = "1";
// //定義span標(biāo)簽
// var span = document.createElement("span");
// //span標(biāo)簽添加文件結(jié)點
// var node = document.createTextNode("1");
// //把文本結(jié)點的數(shù)據(jù)添加到span標(biāo)簽上
// span.appendChild(node);
// //把span標(biāo)簽添加到div里
// document.getElementById("1").appendChild(span);
for (var i = 0; i < jsonPData.length; i++) {
var div = document.createElement("div");
document.getElementById("show").appendChild(div);
div.id = i;
for (key in jsonPData[i]) {
var res = jsonPData[i][key]; //獲取的數(shù)據(jù)
//創(chuàng)建一個span標(biāo)簽
var span = document.createElement("span");
//span標(biāo)簽添加文件結(jié)點
var node = document.createTextNode(res);
//把文本結(jié)點的數(shù)據(jù)添加到span標(biāo)簽上
span.appendChild(node);
document.getElementById(i).appendChild(span);
}
}
}
//html頭部動態(tài)創(chuàng)建一個script標(biāo)簽,通過src屬性獲取外部數(shù)據(jù)
//scr = "http://58city.com/data2.php?jsonp=回調(diào)函數(shù)名";
var script = document.createElement("script");
script.src = "http://58city.com/goods.php?jsonp=handle";
document.head.appendChild(script);
</script>
</html>
<?php
require 'autoLoad.php';
use compotents\conn\DBconn;
$user =new DBconn();
$table = 'tb_goods';//表名
$limit =10; //顯示10條記錄
$records = $user->select($table,'*','*',$limit);
$jsonStr = json_encode($records);
echo "handle($jsonStr)";
?>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號