Jump to content
mathieu

Server-side code not interpreted when called from AJAX

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

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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)

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...