0

I've create my app. When i test on desktop or run on PhoneGap IDE it runs ok and connect with server, but when i build my apk and run it on Android, the request failed and launch this menssage:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

This is my XMLHttpRequest:

sendPost: function(url, data, callback){

        var loading = document.getElementById('loading');
        loading.innerHTML = '<p></p>';
        loading.classList.add('loading_active');

        var params = '';
        var count = Object.keys(data).length;
        for(var i=0; i<count;i++){
            if(i>0){
            params += '&';
            }
            params += Object.keys(data)[i] + '=' + Object.values(data)[i];
        }

        var xhr = new XMLHttpRequest();
        xhr.open('POST', url, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var res = JSON.parse(xhr.responseText);
                if(callback){
                    callback(res);
                }
            }
        };
        xhr.send(params);
    },

And i call it this way:

app.sendPost(url,data,function(res){
    some code.....
    }); // end app.sendPost()

And i've include this line in htaccess file:

Header set Access-Control-Allow-Origin "*"

But server not allow request from Phonegap (localhost)

2 Answers2

0

You should take a look at this post :

Enable cors in htaccess

Setting the header with PHP should do the trick (if ever you are using PHP serverside)

Raphael Deiana
  • 750
  • 4
  • 20
  • Hi, i try this, but now i get this message: `The 'Access-Control-Allow-Origin' header contains multiple values 'null, *', but only one is allowed. Origin 'null' is therefore not allowed access.` – Jorge del Campo Andrade Dec 05 '19 at 19:48
  • Can you post the header function you are using please. – Raphael Deiana Dec 05 '19 at 19:55
  • Sure. I erase htaccess lines and include this code in my PHP files: ` // Access-Control headers are received during OPTIONS requests if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); } ` – Jorge del Campo Andrade Dec 05 '19 at 20:17
  • Can you try with these lines instead of your snippet : header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); header('Access-Control-Allow-Origin: *'); – Raphael Deiana Dec 05 '19 at 20:38
  • Same thing :( I do not know what else to do... When i'm testing my project on Phonegap, it works fine: The problem is when i build my app and test the apk on my phone – Jorge del Campo Andrade Dec 06 '19 at 10:24
  • Solved!! the problem was i must include a whilte list on config.xml phonegap file: – Jorge del Campo Andrade Dec 06 '19 at 13:06
0

Solved!! the problem was i must include a whilte list on config.xml phonegap file:

AS i explain previously, the problem happened only when i run the build project phonegap apk (in development mode runs ok) So i just included a white list on config.xml file en it works.

Inlude this lines in your config.xml file: ´´´ ´´´