关于使用Jsonp做跨域请求
今天在使用Jsonp做跨域请求的练习时碰上这样一个问题
代码如下
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 6 <title>Weather Forcast</title> 7 <meta name="description" content=""> 8 <meta name="keywords" content=""> 9 <link href="" rel="stylesheet"> 10 <script> 11 window.onload = function(){ 12 var oHead = document.getElementsByTagName('head')[0]; 13 var script =document.createElement("script"); 14 script.src = 'http://api.jirengu.com/weather.php?callback=getWeather'; 15 oHead.appendChild(script); 16 oHead.removeChild(script); 17 function getWeather (data){ 18 alert(data.date); 19 } 20 } 21 </script> 22 23 </head> 24 <body> 25 </body> 26 </html>
控制台显示getWeather未定义
结果之后把getWeather定义为全局函数后可以正常使用
修改getWeather函数为
window.getWeather = function(data){ // code }
但是使用jQuery 好像不存在这个问题啊 不知道是什么原因呢