0

I'm trying to feed the output from a php file into a javascript variable. My code looks like this -

PHP file:

<?php
echo "Hello World!";
?> 

HTML file:

<!DOCTYPE html>
<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            var data_from_ajax;
            $.get('http://127.0.0.1/output.php', function(data) {
                data_from_ajax = data;
            });
        </script>
    </body>
</html>

I'm using python's SimpleHTTPServer for temporary testing. The error that returns from the script is Failed to load resource: net::ERR_CONNECTION_REFUSED. What am I doing wrong?

EDIT: So I replaced the url with http://127.0.0.1:8000/output.php, and now the error has disappeared. When I type that URL into chrome, the php file downloads to my computer. Now however, when I try console.log(data_from_ajax), it returns "undefined", and still nothing shows up on the page. What could be going wrong?

0 Answers0