This is the first time for me to use the jQuery $.ajax()
in Drupal. The code is a simple test code but it keeps going to the error:
section instead of success:
. Could my url path be wrong? The .js file and the test-ajax.php
file are in the same folder named "code".
//This is Javascript
$.ajax({
url: 'code/test-ajax.php',
data: 'a=hello&b=world',
type: 'POST',
success: function(data){
alert(data); //Should return "hello, world"
},
error: function(){
alert('FAIL'); //Keep getting "FAIL". It's like it can't find the php file.
}
});
Nothing fancy inside the file test-ajax.php
:
<?php
$a = $_POST['a'];
$b = $_POST['b'];
echo "$a, $b";
?>
EDIT: To avoid confusion on the paths (I keep it organized):
- Main theme files:
<templatename>/<.info, template.php, style.css, etc.>
- Template files:
<templatename>/templates/*.tpl.php
- AJAX files:
<templatename>/code/<PHP and .JS files>
code/
prefix in your url.url: 'code/test-ajax.php'
andurl: 'code/test-ajax.php'
don't seem to work. Isn't that odd? In WP I useurl: TEMPLATEDIR + '/code/test-ajax.php'
is there a Drupal version of that?