Skip to content

Instantly share code, notes, and snippets.

@a1exlism
Last active October 14, 2017 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a1exlism/b9a1a0b1495c03af34e0f02e2d1f5b3e to your computer and use it in GitHub Desktop.
Save a1exlism/b9a1a0b1495c03af34e0f02e2d1f5b3e to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<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>TEST</title>
</head>
<body>
<h1 style="text-align:center;">TEST</h1>
<img id="img" src="" alt="img">
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script src="./jsonp.js"></script>
</body>
</html>
function imgAdd(address) {
$('#img').attr('src', address);
}
window.onload = function () {
setTimeout($.ajax({
url: 'http://t3st.com',
type: 'GET',
dataType: 'jsonp',
crossDomain: true, // 强制跨域
jsonp: 'jsonpRequest', // key
jsonpCallback: 'imgAdd', // value
// jsonp & jsonpCallback 绑定使用 jsonp改变了原来的回调函数名callback
// 关于安全性, 可以设置jsonp: false
// 测试下来差不多就是 jsonp=jsonpCallback
// success: function (data) {
// if (data) {
// console.log(data);
// }
// },
}), 500);
console.log(1);
}
<?php
$img_src = 'http://barkpost-assets.s3.amazonaws.com/wp-content/uploads/2013/11/dogelog.jpg';
$callbackFunc = $_GET['jsonpRequest'];
if (isset ($callbackFunc) && $callbackFunc == 'imgAdd') {
// header('Content-type: application/json');
echo $callbackFunc.'('.json_encode($img_src).')';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment