TheBugger
TheBugger

Reputation: 41

CORS working on my pc with no errors, but not working on any other device

This is my code for CORS . I have uploaded my website on https://in.000webhost.com/(it is a free web hosting service. I have this code in my controller.php page. Now when i try to access the website via the url from my pc , it works fine. But if i use any other device to access the website via the url , the part of the page which I display through an AJAX call to controller.php doesn't work and error is shown which says( refer image)enter image description here The resource is Controller.php ,the same file which has the CORS headers.

 if (isset($_SERVER['HTTP_ORIGIN'])) {
      
      header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");  
    }else{
        header("Access-control-Allow-Origin: *"); 
    }
      header('Access-Control-Allow-Credentials: true');
      header('Access-Control-Max-Age: 86400');    
    
      if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
          
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT");         
        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    
        exit(0);
    }

Upvotes: 0

Views: 1632

Answers (1)

Mohsin Ali
Mohsin Ali

Reputation: 500

You've to update the ajax request URL from

http://localhost/myprojects/test_MaruthiSolar/bootstrap-4.1.3-dist/controller.php

to something like this

https://maruthisolar.000webhostapp.com/myprojects/test_MaruthiSolar/bootstrap-4.1.3-dist/controller.php

Upvotes: 2

Related Questions