一、跨域获取 php 文件数据
- a.html(http://aaa.com/a.html)
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://aaa.com/proxy.php?random=' + Math.random(), true);
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if ((this.status >= 200 && this.status < 300)
|| (this.status === 304)) {
console.log(this.response);
} else {
console.log(new Error);
}
}
}
xhr.send(null);
</script>
</body>
</html>
- b.php(http://bbb.com/b.php)
<?php
header('Content-type: application/json; charset=utf8');
$json = array(
'usernae' => '张三',
'age' => '22',
'domain' => 'http://bbb.com'
);
echo json_encode($json, JSON_UNESCAPED_UNICODE);
?>
- proxy.php(http://aaa.com/proxy.php)【中转代理,后端无同源限制】
<?php
header('Content-type: application/json; charset=utf8');
$crossUrl = 'http://bbb.com/b.php';
$str = file_get_contents($crossUrl);
$data = json_decode(trim($str,chr(239).chr(187).chr(191)),true);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
?>
二. 跨域获取 json 文件数据
- a.html(http://aaa.com/a.html)
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://aaa.com/proxy.php?random=' + Math.random(), true);
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if ((this.status >= 200 && this.status < 300)
|| (this.status === 304)) {
console.log(this.response);
} else {
console.log(new Error);
}
}
}
xhr.send(null);
</script>
</body>
</html>
- b.json(http://bbb.com/b.json)
{
"name": "demo",
"age": "*",
"favorite_fruit": ["apple", "banana"],
"domain": "http://bbb.com"
}
- proxy.php(http://aaa.com/proxy.php)【中转代理,后端无同源限制】
<?php
header('Content-type: application/json; charset=utf8');
$crossUrl = 'http://bbb.com/b.json';
$str = file_get_contents($crossUrl);
$data = json_decode(trim($str,chr(239).chr(187).chr(191)),true);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
?>
三. 使用 JQ 方法
1. $.ajax()
- a.html(http://aaa.com/a.html)
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
</head>
<body>
<script>
$.getJSON('http://aaa.com/proxy.php', 'random=' + Math.random(), function(retval, status, xhr) {
console.log(retval);
});
</script>
</body>
</html>
2. $.getJSON()
- a.html(http://aaa.com/a.html)
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
</head>
<body>
<script>
$.ajax({
type:"get",
url:"http://aaa.com/proxy.php",
async:true,
data:{
'random':Math.random()
},
success:function(retval) {
console.log(retval);
}
});
</script>
</body>
</html>