# Getting response in localhost but getting CORS error on infinityfree hosting server

**URL:** <https://forum.infinityfree.com/t/getting-response-in-localhost-but-getting-cors-error-on-infinityfree-hosting-server/97161>\
**Category:** Hosting Support\
**Created:** [August 9, 2024, 7:34am UTC](https://forum.infinityfree.com/t/getting-response-in-localhost-but-getting-cors-error-on-infinityfree-hosting-server/97161 "2024-08-09T07:34:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon4330384](https://forum.infinityfree.com/letter_avatar/anon4330384/32/6_22734026db12803ebb3e059622ac3534.png) [@anon4330384](https://forum.infinityfree.com/u/anon4330384)\
**Post date:** [August 9, 2024, 7:34am UTC](https://forum.infinityfree.com/t/getting-response-in-localhost-but-getting-cors-error-on-infinityfree-hosting-server/97161/1 "2024-08-09T07:34:33Z")

</div>

### Error Message

Access to XMLHttpRequest at ‘[http://niceday.free.nf/api.php](http://niceday.free.nf/api.php)’ from origin ‘[http://localhost:3000](http://localhost:3000)’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

### Other Information

These are my entire code:  
header(“Content-Type: application/json”);  
header(“Access-Control-Allow-Origin: \*”);  
header(“Access-Control-Allow-Methods: POST, OPTIONS”);  
header(“Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Methods”);

try {

```
if($_SERVER["REQUEST_METHOD"] == "POST" ){
    $data = json_decode(file_get_contents("php://input"), true);
    include '../config/db.php';
    
    $username = $data["username"];
    
    $query = $conn->prepare("SELECT * FROM `user` WHERE `username` = ?");
    $query->bindParam(1, $username, PDO::PARAM_STR);
    if($query->execute()){
        $user = $query->fetch(PDO::FETCH_ASSOC);
        if($user){
            echo json_encode(array("user_id" => $user["id"],"message" => "Logged in Successfully", "status" => true));   
        }else{
            echo json_encode(array("message" => "Wrong Username", "status" => false));    
        }
    }else{
        echo json_encode(array("message" => "Wrong Username", "status" => false));    
    }  
}

```

} catch (exception $e) {  
echo json\_encode(array(“message” =\> “Something Went Wrong!”, “status” =\> false));  
}

?\>

---

<div class="post-metadata">

**Author:** ![anon90933456](https://forum.infinityfree.com/letter_avatar/anon90933456/32/6_22734026db12803ebb3e059622ac3534.png) [@anon90933456](https://forum.infinityfree.com/u/anon90933456)\
**Post date:** [August 9, 2024, 7:37am UTC](https://forum.infinityfree.com/t/getting-response-in-localhost-but-getting-cors-error-on-infinityfree-hosting-server/97161/2 "2024-08-09T07:37:14Z")

</div>

Hi and welcome to the forum!

> [@Ensuring only web browsers can access your website](https://forum.infinityfree.com/t/ensuring-only-web-browsers-can-access-your-website/49353):
>
> InfinityFree is a website hosting service. That means that the hosting accounts we provided are intended for hosting websites. Websites contain pages that are accessed through web browsers. InfinityFree is not intended to be used for file sharing, API hosting, database hosting or background tasks/tools. To help enforce this, free hosting enforces a security system that makes sure that anyone trying to access your website is using a normal web browser. This is done by checking whether the web br…

---

<div class="post-metadata">

**Author:** ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)\
**Post date:** [August 9, 2024, 10:26am UTC](https://forum.infinityfree.com/t/getting-response-in-localhost-but-getting-cors-error-on-infinityfree-hosting-server/97161/3 "2024-08-09T10:26:01Z")

</div>

To give a little bit of context to @anon90933456’s post: our hosting does not support or allow CORS. We provide hosting for websites, not API’s.

If you are building a frontend Javascript application with a PHP backend, you can host it on our hosting, as long as you’re hosting them on the same hostname. And for development, you should run both the Javascript application and the PHP backend locally.

---

<div class="post-metadata">

**Author:** ![system](https://infinityfree-forum-uploads.s3.dualstack.eu-central-1.amazonaws.com/original/3X/b/0/b024fd3fa88bae093c9576e55c2c94e33f890206.svg) [@system](https://forum.infinityfree.com/u/system)\
**Post date:** [August 16, 2024, 10:26am UTC](https://forum.infinityfree.com/t/getting-response-in-localhost-but-getting-cors-error-on-infinityfree-hosting-server/97161/4 "2024-08-16T10:26:15Z")

</div>

Closed on August 16, 2024, 10:26am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
