简体   繁体   中英

How to enable Cross-Origin Resource Sharing on my mobile app (cordova) that hooks up to an API (in Yii)

I have implemented a mobile cordova app (from another external developer) into my local web environment that works inconjunction with a RESTful API system that I have developed.

I am trying to login, this passes the JSON request to my API with the username & password and basically returns a response.

In Mozilla Firefore I get the following response within the console log :-

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://mystickers.local/myStickersApi/V1/appLogin.
This can be fixed by moving the resource to the same domain or enabling CORS.

Can anyone provide the best/simplest way to enable Cross Origin Requestion Sharing, my API is developed using Yii 1.xx (eg PHP) and the mobile app is developed using the Cordova framework (a combination of HTML5/CSS & javascript).

I added the following block of code to my API just before the json content is echo'd out

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
    header("Access-Control-Allow-Headers: Authorization");

Seems to work now :)

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

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