XMLHttpRequest 无法加载 'url'.Access-Control-Allow-Origin不允许使用源“


XMLHttpRequest cannot load 'url'. Origin 'url' is not allowed by Access-Control-Allow-Origin

我在使用此代码时收到此错误。

function like (id){
        $.ajax({
                type: "POST",
                url: "../web/like/" + id + "/",
                success: function(response) {
                    // succeed silently
                },
                error: function(response) {
                    // fail silently
                },
            });
}

现在我已经查看了一大堆具有相同错误代码的问题,但这是在同一个域上提交 ajax 请求,而我看到的所有问题都来自其他来源。

确切的错误消息,XMLHttpRequest 无法加载 URL/web/like/3/。源 URL 不是访问控制允许源

允许的。

把它放在你调用的页面上:

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