localhost中的“Access-Control-Allow-Origin”标头错误?

时间:2015-03-20 12:50:55

标签: php angularjs http

我已添加此标题(' Access-Control-Allow-Origin:*');在我的PHP中,但我还在

'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

我的angularjs POST如下:

$http({
        url: 'http://example.com/url/',
        method: "POST",
        data: { 'myParam' : 'test' }
    })
    .then(function(response) {
            alert('success');
        }, 
        function(response) { // optional
            // failed
        }
    );

3 个答案:

答案 0 :(得分:0)

将此行添加到.htaccess文件中:

Header set Access-Control-Allow-Origin "*"

答案 1 :(得分:0)

您将需要通过将跨域设置为true并将缓存设置为false来添加这两行代码。

crossDomain: true,
cache:false,

因此您的完整代码将变为

$http({
        url: 'http://example.com/url/',
        method: "POST",
        data: { 'myParam' : 'test' },
        crossDomain: true,
        cache:false
    })
    .then(function(response) {
            alert('success');
        }, 
        function(response) { // optional
            // failed
        }
    )

答案 2 :(得分:-2)

Chrome是本地开发的一个错误。尝试其他浏览器。然后它会工作。

相关问题