请求的资源上没有“Access-Control-Allow-Origin”标头,但未使用Php标头

时间:2015-07-25 12:47:24

标签: php ajax xmlhttprequest

我想用Ajax获取这样的URL:

$.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 No 'Access-Control-Allow-Origin' header is present on the requested resource error

建议将此代码放在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";
    ?>

如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

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

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