Jump to content

Server-side code not interpreted when called from AJAX


mathieu

Recommended Posts

Hi, I'm trying to build a "hello world" AJAX / PHP application: AJAX code:

<html><head>  <title></title>  <script src="http://code.jquery.com/jquery-latest.js"></script>  <script>   $(document).ready(function(){	//Requete AJAX	var resultat = $.ajax({	 url: "serveur.php",	 async: false	});	alert(resultat.responseText);   });  </script></head><body></body></html>

PHP code:

<?phpecho 1;?>

I would expect the result of the alert() function to be "1", while it is:<?phpecho 1;?> So as far as I understand, the server-side code is not interpreted, and that's surprising to me since it is interpreted when I run it separately, without calling from AJAX. May I ask for your help? Thanks, Mathieu

  • Like 1
Link to comment
Share on other sites

That doesn't make sense. I can't imagine any situation in which this could happen. Wherever the problem is, it's not with the Javascript. Are you testing this in a proper server environment?

Link to comment
Share on other sites

Is it possible that when you run the file normally it goes through Apache, but when you get it through AJAX, it comes through the Mac file system? If the browser and the server are on the same machine, I can see how that might happen. If the server is remote, then probably not.

Link to comment
Share on other sites

Indeed, both client and server are on the same machine. In order to test whether this assumption is correct or not, I've uploaded the PHP file on a remote server. When I call the PHP direct through the browser, it works fine. However, when I call it through AJAX, I catch an exception:- NETWORK_ERR: XMLHttpRequest Exception 101 (Chrome)- Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) (Firefox)

Link to comment
Share on other sites

The AJAX has to be on the same server as the PHP file, and in the case you're running it from your computer, you have to access it with an http: address.

Link to comment
Share on other sites

PHP requires a web server to run, the server is what executes the PHP code. If you just double-click on an HTML file to open it in a browser, you're not using a web server. You're just using the browser to open a local file on your computer. So when it requests a PHP file there's nothing to execute the PHP code, it just opens the file and displays it. When a web server is involved then it receives the request for a PHP page, the web server is configured so that it knows that PHP pages need to be sent to PHP to get executed, so it passes the code to PHP and sends the output to the browser. The browser doesn't receive the PHP source code, it receives the output of the PHP code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...