php中缺少CORS标题'Access-Control-Allow-Origin'

时间:2016-11-08 09:55:09

标签: php ajax cross-domain

在我的网络应用程序中,尝试使用ajax调用将数据传递到另一个域。但它显示的错误如Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://example.com/auth/webschool/test.php. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

我的ajax代码是,

 $.ajax({
                type: "POST",
                url: "http://example.com/auth/webschool/test.php",
                data: {username: "abcd",password:"abcd"},
                 dataType: "html",
                success: function (data) {
                 alert(data);
                }
            });

1 个答案:

答案 0 :(得分:2)

只需将此行放在test.php上,希望它能正常工作

<?php header('Access-Control-Allow-Origin: *'); ?>