Jump to content

Access-Control-Allow-Origin'


lilmer

Recommended Posts

Hello good day,

 

I have a problem running an HTML5 mobile app. I am using Intel XDK as an IDE to build an android mobile application, on my desktop simulator of Intel XDK, AJAX call are working fine but when I to run the app on my mobile device using USB debugger ajax call is not working, it gives me an error of 

XMLHttpRequest cannot load https://www.site.com/index.php?mobile/loginauthenticate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access. 

on my server side code I already put

header('Access-Control-Allow-Origin', "*");

as it is needed to accept data from cross browsing, but still I get the same error, anyone that knows what did I done wrong ?

Here's my ajax request code:

 
var URL = 'https://www.site.com/index.php?mobile/';
$.ajax({
                type:"POST",
                url:URL+'loginauthenticate',
                data:{email:email,password:password},
                dataType:'json',            
                beforeSend: function(){
                 $.ui.showMask('Logging in...');  
                }
            }).success(function(data){    
              
                var object = data;
                   if(object.result === 'yes'){
                       $.ui.manageHistory=false;
                        sessionToken = object.sessionToken;
                        logged = true;
                        localStorage.setItem('sessionToken',sessionToken);
                        localStorage.setItem('logged',true);
                        localStorage.setItem('email',email);
                        $.ui.hideMask();  
                        //$.ui.loadContent("#account_page");
                        processAccount(sessionToken);
                   }else{
                       $.ui.hideMask();  
                      alert(data.error);
                   }
                 console.log(object);
                    
            }).error(function(xhr, status, error){
                 $.ui.hideMask();   
                console.log(xhr);      
                 console.log(status);      
                 console.log(xhr);      
               
            });

Thanks

Link to comment
Share on other sites

    public function cors(){


            // Allow from any origin
            if (isset($_SERVER['HTTP_ORIGIN'])) {
                header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
                header('Access-Control-Allow-Credentials: true');
                header('Access-Control-Max-Age: 86400');    // cache for 1 day
            }

            // 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, OPTIONS");         

                if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
                    header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

                exit(0);
            }

    }

public function loginauthenticate(){
        $this->cors();
        header('Access-Control-Allow-Origin', "*");
     
}

I did not include the authentication function for validating the POST request. I think the problem is when trying to request on that URL.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.