-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Cors not working #9442
Comments
Is this your case #9437 ? |
This is not a CodeIgniter issue. Are you using Vite? Have you set |
idk what is problem but i can fix
i am just add this file and not problem with vite
i think file settings cors file is not working |
I made a standard Vite installation for Vue. My <template>
<div>
<h1>Vite + Vue + CORS Test</h1>
<button @click="fetchData">Fetch Data from API</button>
<p>{{ apiResponse }}</p>
</div>
</template>
<script>
export default {
data() {
return {
apiResponse: '',
};
},
methods: {
async fetchData() {
try {
const response = await fetch('http://localhost:8080/api/v1/auth/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: 'Hello from Vue' }),
});
const data = await response.json();
this.apiResponse = JSON.stringify(data);
} catch (error) {
this.apiResponse = 'Error: ' + error.message;
}
},
},
};
</script>
My sample <?php
namespace App\Controllers\Api;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
class AuthController extends ResourceController
{
public function signup(): ResponseInterface
{
$message = $this->request->getJsonVar('message');
$data = ['sample' => 'data', 'postMessage' => $message];
return $this->respond($data, 200);
}
} Then I applied routes and cors config from your initial post. Everything is working fine, so I cannot reproduce the problem you're reporting. In your second post, you're using also "authApiFilter" filter, so I'm guessing this is the issue - just like neznaika0 said. Are you experiencing the same problems only when you're using the default |
now i again test Cors.php file and that corrcet idk what is problem but now is fix my cors settings
I think the problem was with the application cache. If you add an option that is in the env and automatically clears the cache between each serve request, it will be fixed. |
Well, if everything is working, then I will close this issue. But feel free to continue the conversation if you notice something new. |
PHP Version
8.2
CodeIgniter4 Version
4.6.0
CodeIgniter4 Installation Method
Composer (using
codeigniter4/appstarter
)Which operating systems have you tested for this bug?
Windows
Which server did you use?
apache
Database
mysql
What happened?
I am using CodeIgniter 4 for the backend as an API for a Vue.js site, but I am encountering a CORS error when trying to communicate between them.
How can I fix this issue?
Steps to Reproduce
I have already applied the following configurations:
Cors.php file
Routes file
Controller Class
I am using:
class AuthController extends ResourceController
However, I am still getting a CORS error, and I can't resolve it.
For running the backend and frontend, I use:
Expected Output
Anything else?
No response
The text was updated successfully, but these errors were encountered: