数据包含php文件是源代码[重复]

This question already has an answer here:

I am trying to confirm the connection between a html file and a php file using ajax :

 $.get(

        'database_query.php?begin='+$("#begin").val(), // Le fichier cible côté serveur.

         'false', // Nous utilisons false, pour dire que nous n'envoyons pas de données.

         function(data,status){

            if(data == 'Success'){

                 // Le membre est connecté. Ajoutons lui un message dans la page HTML. 

                alert("<p>Vous avez été connecté avec succès !</p>");

            }

            else{
                 // Le membre n'a pas été connecté. (data vaut ici "failed")

                 alert("<p>Erreur lors de la connexion...</p>"+data+" "+status);

            }

        } // Nous renseignons uniquement le nom de la fonction de retour.
        ,
        'text' // Format des données reçues.

       );

however what i get in return in data is the hole source code of the triggered php file and not "Success" or "Failed" !?

this is the php file triggered :

<?php

try
 {
// On se connecte à MySQL
$bdd = new PDO('mysql:host=localhost;dbname=nyc;charset=utf8', 'root', '');
}

catch(Exception $e)
{
// En cas d'erreur, on affiche un message et on arrête tout
   die('Erreur : '.$e->getMessage());
}


if(isset($_GET["begin"])){

    echo "Failed";

}
else
    echo "Success";
?>

Note:status contains success ! and in the console i get this error :

XML Parsing Error: no root element found
Location: file:///C:/xampp/htdocs/nyc-project/database_query.php?begin=2018-07-11
Line Number 33, Column 3:

what is wrong ? how to fix ?

</div>

I think the problem is that your PHP server is not work well.You can write a php file just like

<?php
phpinfo();
?>

Then you can visit this file by web browser, and what can you see? If you can see the php information, Your server is work well, But if you can the code source code, It means you should set your server

Maybe you could try this one :

<!doctype  html>
<html lang="fr">
<head>

    <title>Test</title>
    <meta charset="utf-8" />

</head>
<body>

    <script src="jquery-3.3.0.min.js"></script>
    <script>

        (function($) {

            var jqxhr = $.get(
                'database_query.php?begin='+$("#begin").val(), 
                function(data) {
                    if(data == 'Success'){
                        // Le membre est connecté. Ajoutons lui un message dans la page HTML. 
                        alert("<p>Vous avez été connecté avec succès !</p>");
                    }
                    else{
                        // Le membre n'a pas été connecté. (data vaut ici "failed")
                        alert("<p>Erreur lors de la connexion...</p>"+data);
                    }
                }
            )
            .fail(function(data) {
                console.log(data);
                alert(data.responseText);
            });

        })(jQuery);

    </script>

</body>
</html>

If you get into the fail function and if you display php errors, you will see why it isn't working.

More details on jQuery.get() and promises on this page : https://api.jquery.com/jquery.get/

Apparently i tracked the source of the problem with the aid of this issue

the problem is in the url :

instead of

'database_query.php

i should put

'localhost/nyc/database_query.php'

after this the previous error disappeared and new error appeared :

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///C:/xampp/htdocs/nyc-project/localhost/nyc/database_query.php?begin=x. (Reason: CORS request not http).