0

I trying post data to backend from Vue app and I have problem with CORS. I using CodeIgniter 4 in backend.

This is my vue service:

import axios from "axios";

const apiUrl = "http://localhost:8080/admin/users/";

export default {

    create(userData) {
        return axios.post(apiUrl + "create", {
            firstName: userData.firstName,
            lastName: userData.lastName,
            email: userData.email,
            password: userData.password,
            role: userData.role
        }).then(response => {
            return response.data;
        }).catch(function() {
            const response = {
                status: 500,
                error: true,
                message: "Błąd połączenia z API.",
                data: []
            };

            return response;
        });
    }

}

Info from console:

Access to XMLHttpRequest at 'http://localhost:8080/admin/users/create' from origin 'http://localhost:8081' 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.

I try add this code in public/.htaccess but it doesn't work:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "http://localhost:8081"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE"
</IfModule>
Lidia K.
  • 209
  • 3
  • 13

1 Answers1

0
  1. Try:
<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "http://localhost:8081"
    Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE"
</IfModule>
  1. Enable apache module headers and restart the Apache service.

Apache: difference between "Header always set" and "Header set"?

Enable mod_headers in Apache in Windows

steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34