jquery jsonp 跨域请求实例

来源:互联网 发布:linux查看文件大小 编辑:程序博客网 时间:2024/04/29 08:06

利用jquery php来实现js的跨域请求


html代码:


[html] view plaincopyprint?
  1. <html>  
  2. <script src="jquery.min.js" type="text/javascript" ></script>  
  3. <script>  
  4. function pushstar(){  
  5. var reval'';  
  6. var host = 'www.xxx.com';  
  7. //editor.sync();  
  8. var stardocument.getElementById('content');  
  9. value ='这里设置内容';  
  10. $.ajax({  
  11. async:false,  
  12. type: 'GET',  
  13. url: "http://" + host +"/index.php" ,  
  14. data:{param:value} ,  
  15. jsonpCallback:"callbak",  
  16. dataType:'jsonp',  
  17. jsonp: 'callbak',   
  18. error:function(request){  
  19.   
  20. //var jsona = $.parseJSON(msg);  
  21. alert('fail');  
  22.   
  23.   
  24. },  
  25. success:function(msg){  
  26. if(msg[0] == 1){  
  27. alert(msg[1]);  
  28. }else{  
  29. alert('fail');  
  30. }  
  31.   
  32.   
  33. },  
  34.   
  35.   
  36.   
  37. });  
  38.   
  39.   
  40. }  
  41. </script>  
  42. 点击<input type="button" onclick="pushstar()">  
  43. </html>  



index.php代码:
 

[php] view plaincopyprint?
  1. <?php  
  2. /* 
  3. *回调文件 
  4.          */  
  5. $arr[] =1;  
  6. $arr[] ='cuccess';  
  7. echo 'callbak('.json_encode($arr).')';exit;  
  8.   
  9. ?>  
0 0