AJAX CORS error

Hello,

I always get the CORS error when getting data from a php file using GET (ajax jquery), the php file and js file is stored locally. Does anyone have the same issue? On another php file it works with the same script just with another POST…

Name: epiz_31877454
Error Message:
Access to XMLHttpRequest at ‘https ://errors.infinityfree.net/403/’ (redirected from 'MYLINK) from origin ‘MY DOMAIN’ 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.
GET https:// errors.infinityfree.net/403/ net::ERR_FAILED
send @ jquery-3.6.0.js:10109
ajax @ jquery-3.6.0.js:9690
retrieveData @ obrd-script.js:8
(anonym) @ index.php?page=leaderboard:29
errors.infinityfree.net/:1
GET https:|/ /errors.infinityfree.net/errors/404/ 404

js:

    $.ajax({
        url: 'includes/functions/getLeaderboardData.php',
        type: 'GET',
        async: true,
        data: {getLeaderboard : 'true'},
        success: function (data) {
         output += data
        }
    });
php:
session_start();
include_once('../../includes/functions/db_credentials.php');
include_once('../../includes/functions/db_connection.php');

if(isset($_GET["getLeaderboard"]))
{
    $SQL = "SELECT idUsername, dtCredits FROM tbluser ORDER BY dtCredits DESC";
    $result = mysqli_query($dbc, $SQL);


    if(mysqli_errno($dbc))
    {
        die("wrong SQL: $SQL Error: " . mysqli_error($dbc));
    }
    for($i = 0; $i < mysqli_num_rows($result); $i++)
    {
        $row = mysqli_fetch_assoc($result);
        echo"<tr>
                     <td>{$row["idUsername"]}</td>
                     <td>{$row["dtCredits"]}</td>
            </tr>";
    }

}

You’ll have to move the file to a different folder. On free hosting, any file with a pathname containing includes returns a 403.

6 Likes

yes that worked, many thanks for your solution. :slight_smile:

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