CORS Error php react map Access to XMLHttpRequest at '/APIs/test/museum/getData.php' from origin 'https://ift.tt/cETpDi5' has been blocked by CORS - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, August 13, 2023

CORS Error php react map Access to XMLHttpRequest at '/APIs/test/museum/getData.php' from origin 'https://ift.tt/cETpDi5' has been blocked by CORS

map#:1 Access to XMLHttpRequest at 'https://ift.tt/Nc0PIVR' from origin 'https://ift.tt/cETpDi5' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

server: litespeed if it matters, but we cannot change it settings

also, we've fixed this error on our previous project, but don't remember how to do it.

react code:

useEffect(() => {
    const fetchData = async () => {
      try {
        const data = '{"request_type": "list"}';
        const config = {
          method: "post",
          maxBodyLength: Infinity,
          url: "https://museum.aveteam.org/API/getData.php",
          headers: {
            "Content-Type": "application/json",
            Authorization:
              "Bearer ",
          },
          data: data,
        };

        const response = await axios.request(config);
        setApiData(response.data);
        console.log(response.data);
      } catch (error) {
        console.error("Error fetching data:", error);
      }
    };

    const fetchData2 = async () => {
      try {
        const data = '{"request_type": "list"}';
        const config = {
          method: "post",
          maxBodyLength: Infinity,
          url: "https://aveteam.org/APIs/test/museum/getData.php",
          headers: {
            "Content-Type": "application/json",
            Authorization:
              "Bearer ", 
          },
          data: data,
        };

        const response = await axios.request(config);

        console.log(response.data);
      } catch (error) {
        console.error("Error fetching data:", error);
      }
    };

    fetchData();
    fetchData2();
  }, []);

php code(headers):

<?php
// $http_origin = $_SERVER['HTTP_ORIGIN'];
//
// if ($http_origin == "https://museum.aveteam.org") {
//   header("Access-Control-Allow-Origin: $http_origin");
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization, Origin, X-Requested-With');
header('Access-Control-Max-Age: 86400');
header('Access-Control-Allow-Credentials: true');
header('content-type: application/json; charset=utf-8');

so request to https://museum.aveteam.org/API/getData.php is working, but request to https://aveteam.org/APIs/test/museum/getData.php makes CORS error. Why? is there any possibility to change this?

added headers


header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization, Origin, X-Requested-With');
header('Access-Control-Max-Age: 86400');
header('Access-Control-Allow-Credentials: true');
header('content-type: application/json; charset=utf-8');



source https://stackoverflow.com/questions/76890510/cors-error-php-react-map-access-to-xmlhttprequest-at-apis-test-museum-getdata

No comments:

Post a Comment