Hello i’ve got this problem with an ajax call in my (newbie) Drupal site. I’m trying to save some data from a form field, by posting it with jQuery to a function in my Drupal module. Here is my code:
// in drupal
function mymodule_menu() {
$items = array();
$items['mymodule/set/data'] = array(
'page callback' => 'mymodule_set_data',
'type' => MENU_SUGGESTED_ITEM,
'access arguments' => array('access content'),
);
return $items;
}
function mymodule_set_data($var) {
drupal_json_output(array('status' => 'OK', 'data' => "return_something"));
}
// in my js file
jQuery("#form_element").on('blur',function(){
jQuery.ajax({
type: 'POST',
url: "mymodule/set/data",
dataType: 'json',
data:{
fu: 'bar'
},
success: function(data) {
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown){
console.log(errorThrown);
}
});
});
Everything goes well, the jQuery get’s triggerd, the ajax call is being catched on the server, and i do get the {“status”:”OK”,”data”:”return_something”} back from the server. Except for the fact that the status of the call is a 404… 🙁
I found an solution to my problem.
Ik was moving my app to an other host and there for i needed to change the url the ajax action was calling. I decided to make it dynamic with the following code:
I changed my ajax call url into:
“http://”+Drupal.settings.url + “/set/data
This resulted in my JS calling the following URL:
http://host/drupal/?q=mymodule/set/data
instead of the old one:
http://host/drupal/mymodule/set/data
Which was something Drupal did like 🙂
Still i don’t understand why my drupal accepted http://host/drupal/mymodule/set/data, handled my data but returned a 404.