简体   繁体   中英

Enabling Cors in Firefox

In angular js , i have write down the following code to call a api .

    $http({
        method: 'GET',
        url: 'https://www.example.com/api/v1/page',
        params: 'limit=10, sort_by=created:desc',
        //headers: {'Content-Type': 'application/json'}
        header:{'Access-Control-Allow-Origin': '*'}
    }).success(function(data){
        alert(data);
    }).error(function(){
        alert("2 error");
    });

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at "url"

It is totally up to a server whether it supports CORS or not. You cannot do this from a requesting browser. It is the server that decides whether it wants to serve cross-origin requests or not.

So, if a server is not configured to allow cross-origin requests, then a browser cannot change that.

Another method of making cross origin requests is JSONP, but again the server must specifically support JSONP requests for them to work.

If you have your own server, then you can make a request of your own server to act as a proxy to obtain the request from the other server and then give it back to you. Server to server requests are not limited by cross-origin (that is a browser thing only). This is less efficient, but is sometimes the only work-around if the original server does not support any cross origin access and cannot be changed to support it.

If you are using an api that is intended to be used from other web sites, then it likely does support some sort of cross origin access and you should find out how that is intended to work from whatever documentation exists for that api. If you are using an api that is not intended to be used form other web sites, then you're out of luck unless you code your own server proxy that is served from the same server that hosts your web page. This is done this way by design so it is not easy to use someone else's API in your web site if they didn't specifically permit this type of cross origin access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM