跨域问题解决

跨域问题出现源于:1、协议号不同;2、域名不同;3、端口号不同;

第一种后台处理:在php文件头部写上 header('Access-Control-Allow-Origin:调用本文件的网站域名');

第二种jsonp处理:

前端操作

    $.ajax({
            url:"xxxxxxxxxxxxxxxxxxxxx",
            dataType:'jsonp',
            data:{name:'zsh'},
            jsonp:'callbacks',
            success:function(result) {
                console.log(result)
            },
            error:function (res) {
                console.log(res)
            }
        });

  后端配合操作

<?php
   //服务端返回JSON数据
   $arr=array('a'=>$_GET['name']);
   $result=json_encode($arr);
   $callback=$_GET['callbacks'];
   echo $callback."($result)";
?>
 
原文地址:https://www.cnblogs.com/zshno1/p/10002619.html