<tfoot id="scysq"></tfoot>
<tfoot id="scysq"></tfoot>
<strike id="scysq"></strike>
  • <ul id="scysq"><sup id="scysq"></sup></ul>

    PHP Ajax 跨域問題最佳解決方案

    更新時間:2022-04-18 編輯:創(chuàng)始人 關(guān)注人次:0 云搜索


    本文通過設(shè)置Access-Control-Allow-Origin來實現(xiàn)跨域。

    例如:客戶端的域名是client.runoob.com,而請求的域名是server.runoob.com。

    如果直接使用ajax訪問,會有以下錯誤:

    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.

    1、允許單個域名訪問

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

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

    2、允許多個域名訪問

    指定多個域名(http://client1.runoob.com、http://client2.runoob.com等)跨域訪問,則只需在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、允許所有域名訪問

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


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


    本文地址: http://www.770551.com/show-243.html ,轉(zhuǎn)載請注明出處。

    最新動態(tài)

    相關(guān)資訊

    服務(wù)支持

    我們珍惜您每一次在線詢盤,有問必答,用專業(yè)的態(tài)度,貼心的服務(wù)。

    讓您真正感受到我們的與眾不同!