'Access-Control-Allow-Origin' header 不存在于请求的资源上,未通过 Php header 解决

No 'Access-Control-Allow-Origin' header is present on the requested resource not solved with Php header

我想要 URL 和 Ajax 像这样:

$.ajax({url: "http://techparty.ir/e.php", success: function(result){
        $("#div1").html(result);
    }});

错误是这样的:

XMLHttpRequest cannot load http://techparty.ir/e.php?_=1437826051819. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://domain.com' is therefore not allowed access.

因为我试图搜索这个问题。但我不能,例如这些链接: https://www.daniweb.com/web-development/php/threads/461902/php-header-is-not-working-for-access-control-allow-origin

建议将此代码放在 Php 文件的顶部:

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

但是没用。这是我的源代码(e.php):

<?php
    header("Access-Control-Allow-Origin: *");   
    header("Access-Control-Allow-Credentials: true ");
    header("Access-Control-Allow-Methods: OPTIONS, GET, POST");
    header("Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, 
        X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");
    echo "ehsan";
    ?>

如何解决这个问题?

只需将此代码添加到 .htaccess 文件:

<IfModule mod_headers.c>
   Header set Access-Control-Allow-Origin "*"
 </IfModule>