2

I 'm working at with XAMPP server at my home pc.My files are in xampp/htdocs/marfo I am trying to fetch some records in a booking database lying on xampp\mysql\data\marfo. Althought i can read all others tables of the same database (using different files at the same directory) this specific table gives me this error "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///C:/xampp/htdocs/marfo/booking.php. (Reason: CORS request not http)."

the code in js is:

function readres() {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            if (this.responceText != "NoResults") {
                reservarray = JSON.parse(this.responseText);
                // function to set whole site for this language


            } else { return "Bad thing !!! Something went wrong .........." }

        } else { return "Didn't found anything malaka.........." }
    }

    xmlhttp.open("GET", "booking.php", true);
    xmlhttp.send();

}

and the code at PHP file:


$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'marfo';

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed:" . $conn->connect_error);
}

$sql = "SELECT * FROM bookings";
$result = $conn->query($sql);
$counter = 0;
if ($result->num_rows > 0) {

    // output reservations
    while ($row = $result->fetch_assoc()) {
        $reservarray = array(
            "datefrom" . $counter => $row["datefrom"], "dateto" . $counter => $row["dateto"], "name" . $counter => $row["name"]
        );
        $counter = $counter + 1;
    }
    echo json_encode($reservarray);
} else {
    echo 'No results';
}

$conn->close();```

any help appreciated....
  • 1
    You've accessed the page runnig this request directly from the file system, use a `http` address to enter the page, then there will be no CORS problems. – Teemu Jul 16 '20 at 15:36
  • check this out: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing – gpl Jul 16 '20 at 15:37
  • i did use http. no error message but still doesn;t response anything. keep staying in readyState 1 with no error.....:-( – Spyros Triantafyllos Jul 16 '20 at 15:40
  • Typo: `responceText`. Another typo: `NoResults` will never match `echo 'No results'; ` – Quentin Jul 16 '20 at 15:44
  • Are you sure about `readyState` staying at 1? That tells, that you never called `xmlhttp.send()`. – Teemu Jul 16 '20 at 15:45
  • Quentin thank you, you are right. But still this is not causing the problem...... – Spyros Triantafyllos Jul 16 '20 at 15:51
  • The problem is that at all similar situations with the same code from the same directories i access all the 6 rest tables of this database. – Spyros Triantafyllos Jul 16 '20 at 15:54
  • The request works as it is (when the page is opened with http://), but you'd never see it working (as it is in the question), the return value from a callback will go to the bitspace. What ever you do with the server response, you've to do it in the callback, or in another function you call from the callback. See https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – Teemu Jul 16 '20 at 16:10
  • THANK YOU ALL. PROBLEM SOLVED WITH xmlhttp.open("GET", "http ://localhost/marfo/book.php", true); AND DUE TO https://www.w3.org/wiki/CORS_Enabled – Spyros Triantafyllos Jul 16 '20 at 17:53

0 Answers0