localhost is therefore not allowed access

前端 未结 3 1012
梦如初夏
梦如初夏 2021-01-20 11:19

To solve CORS issue, I wrote there

header(\'Access-Control-Allow-Origin: *\');
header(\'Access-Control-Allow-Methods: GET, POST\');
header(\"Access-Control-A         


        
3条回答
  •  隐瞒了意图╮
    2021-01-20 11:27

    Due to browser security restrictions, most Ajax requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, port, or protocol. But Script and JSONP requests are not subject to the same origin policy restrictions.

    If you have n't used JSONP yet. The Wikipedia Says

    JSONP or “JSON with padding” is a complement to the base JSON data format, a usage pattern that allows a page to request and more meaningfully use JSON from a server other than the primary server.

    So your ajax call should be like this :

    $.ajax({
            type: 'GET',
            crossOrigin: true,
            dataType: "jsonp",
            url: url,
            success: function(data) {
                console.log(data);
            }
        });
    

提交回复
热议问题