1. <del id="beud3"></del>
      1. <dfn id="beud3"></dfn>
      2. PHP實(shí)現(xiàn)跨域解決方法

        摘要

        PHP實(shí)現(xiàn)跨域解決方法,如果要實(shí)現(xiàn)跨域通過(guò)設(shè)置Access-Control-Allow-Origin來(lái)實(shí)現(xiàn)跨域。


        如果要實(shí)現(xiàn)跨域通過(guò)設(shè)置Access-Control-Allow-Origin來(lái)實(shí)現(xiàn)跨域。

        例如:客戶(hù)端的域名是client.runoob.com,而請(qǐng)求的域名是server.runoob.com。

        如果直接使用ajax訪問(wèn),會(huì)有以下錯(cuò)誤:


        XMLHttpRequest cannot load http://server.runoob.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.runoob.com' is therefore not allowed access.


        1538273162157019.jpg

        1、允許單個(gè)域名訪問(wèn)

        指定某域名(http://client.runoob.com)跨域訪問(wèn),則只需在http://server.runoob.com/server.php文件頭部添加如下代碼:

        header('Access-Control-Allow-Origin:http://client.runoob.com');

        2、允許多個(gè)域名訪問(wèn)

        指定多個(gè)域名(http://client1.runoob.com、http://client2.runoob.com等)跨域訪問(wèn),則只需在http://server.runoob.com/server.php文件頭部添加如下代碼:
        $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';  
          $allow_origin = array(  
            'http://client1.runoob.com',  
            'http://client2.runoob.com'  );  
          if(in_array($origin, $allow_origin)){  
            header('Access-Control-Allow-Origin:'.$origin);       }

        3、允許所有域名訪問(wèn)

        允許所有域名訪問(wèn)則只需在http://server.runoob.com/server.php文件頭部添加如下代碼:

        header('Access-Control-Allow-Origin:*');


        PHP實(shí)現(xiàn)跨域解決方法,php,PHP實(shí)現(xiàn)跨域   時(shí)間:   閱讀:   來(lái)源:

        欧美性爱中文字幕在线播放,亚洲av片不卡无码影视,国产av原创国片精品有毛,亚洲成av人无码综合在线观看 欧美一级婬片人妻欧美大 亚洲v日韩v精品v下载

        1. <del id="beud3"></del>
          1. <dfn id="beud3"></dfn>