views:

376

answers:

4

Hi. I am trying to use jquery's ajax $.get(...) function to send a request to my server and have it return some data. I am using the following code:

$.get("php/getRocks.php", { name: "John", time: "2pm" },
    function(data){
    alert("Data Loaded: " + data);
});

Instead of getting the the data back, it just returns the entire php file as a string. Am I doing something wrong? Thanks for the help.

+8  A: 

Is PHP installed on your server? This is a server issue. For some reason your .php file isn't being handled properly and it is returning the PHP code in plain text.

This is in response to:

Well, I seem to have solved the problem, sort of. Apparently, if I access the site localy, php doesn't work, but if I use the domain name, it does. Anyone know why? Or better yet, a way to fix this?

Thanks to everyone for the help!

When working locally (and it always is a good idea to do so before uploading to a live environment) you need to setup PHP on your computer so that it can run the pages you require. A browser will not do it for you as it is a server side technology. You can download a package like Uniform Server, that will give you a full server environment for you to work with.

Sam152
A: 

1st. make sure you have php installed

if u r using linux / apache2 and php 5 u need to compile/install apache and load php via dso eg:

LoadModule php5_module        modules/libphp5.so

IMHO it got nothing todo with your jquery but your web server installation/config:

if it is apache make sure u add this line in your conf file:

AddType application/x-httpd-php .php

more reading about php+apache if u r using php anad apache in linux/nix eg: http://dan.drydog.com/apache2php.html

Abu Aqil
I do use apache and already have that line in the conf file. I don't think its an issue with my server setup cause this used to work fine with plain javascript. This has only started happening since i started messing around with jquery
bobbyb
opening php/getRocks.php directly in your browsers will also tell something if everything oks.
Abu Aqil
When I try to access it from the browser, I also get the php code back :(
bobbyb
meaning, it has nothing to do with jQuery :/
Moak
maybe it s time to recheck your installations,configurations for apache+php
Abu Aqil
so it would seem. I just tried my old code and it does the same thing... :(
bobbyb
i'm very confused now. I use phpMyAdmin on my server and that works fine
bobbyb
how about php script tags <? ?> or <?php ?> ...
Abu Aqil
A: 

Well, I seem to have solved the problem, sort of. Apparently, if I access the site localy, php doesn't work, but if I use the domain name, it does. Anyone know why? Or better yet, a way to fix this?

Thanks to everyone for the help!

bobbyb
If you are going to C:\My Websites\something.php then it won't process the php. You have to access it from your server at http://localhost or wherever your files are.
rpflo
A: 

I have the same problem (using MAMP on mac), and while I haven't solved it, I am a step closer.

Basically, it would appear to have something to do with subdirectories/document root. If I set my url as getRocks.php (and move my script there), it works (I get content back), but if I get it as php/getRocks.php, it does not work (I get the php code back).

Perhaps MAMP is doing something add with response types within subdirectories? Am going to take a further look, but hopefully this helps lead you in the right direction.

jeffthink
actually, to be clear above, I should have used example lib/getRocks.php or subdir/getRocks.php as compared to php/getRocks.php (the idea that it's the subdir that's the problem, not anything with php in particular)
jeffthink