<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title></title>
<script type="text/javascript" src='../jquery.js'></script>
<script type="text/javascript">
$(function(){
$('button').click(function(){
//生成script标签,且src里面的内容应该是http://localhost/2.php?callback=fn
var script=document.createElement('script'); //dom对象
$('script').attr('src',"http://localhost/2.php?callback=fn");
//把script标签添加到header头中
$('head')[0].appendChild(script);
});
});
</script>
<script type="text/javascript">
function fn(msg){
alert(msg)
}
</script>
<!-- jsonp的底层实现原理 ,主要利用script标签的src属性指向要跨域请求的地址
<script type="text/javascript" src="http://localhost/2.php?callback=fn"></script> -->
</head>
<body>
<button>点击</button>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title></title>
<script type="text/javascript" src='../jquery.js'></script>
<script type="text/javascript">
$(function(){
$('button').click(function(){
//生成script标签,且src里面的内容应该是http://localhost/2.php?callback=fn
var script=document.createElement('script'); //dom对象
$('script').attr('src',"http://localhost/2.php?callback=fn");
//把script标签添加到header头中
$('head')[0].appendChild(script);
});
});
</script>
<script type="text/javascript">
function fn(msg){
alert(msg)
}
</script>
<!-- jsonp的底层实现原理 ,主要利用script标签的src属性指向要跨域请求的地址
<script type="text/javascript" src="http://localhost/2.php?callback=fn"></script> -->
</head>
<body>
<button>点击</button>
</body>
</html>