How to allow cross-origin requests?

Asked

Viewed 3,081 times

3

I have an App on Swift that consumes data from an API, on Swift I had to enable on info.plist adding:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Dai need to enable the same in angular, how is it? I saw in some places saying that you need to do something on the server but I didn’t even need...

Message:

Blocked cross-origin request: Same origin policy (Same Origin Policy) prevents reading the remote resource on http://jsonplaceholder.typicode.com/comments. (Reason: the CORS header 'Access-Control-Allow-Origin' is not present).

2 answers

1

To deactivate the same origin policy try to start Chrome with the following flags:

--disable-web-security --user-data-dir

Source

1

Hello what programming language you are using on your server (web services/API)?

I’ll leave an example below in php and can be done easily in other languages.

To enable CORS just return a header on the requested page the header is the Access-Control-Allow-Origin and its value is *

Example in PHP

<?php
        //habilitando o cors
        header("Access-Control-Allow-Origin: *");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.