I'm new with ajax and js. I'm trying to learn a bit about it by AJAXing up a drupal site I'm building. I found this module when I noticed that my scripts and css wasn't loading for pages called via ajax. I have stared at the example included with the module and have figured out what it does for the most part. I copied the example js file and have been modifying it to suit my needs. Everything works until the data is returned. At that point, it appears that the if statement for the callback fails to evaluate to true. I cannot figure out what is wrong with the callback or the data and need some pointers. Can you help me out?
Here is the .ajax call from my js file... the alert('Success') fires successfully.
$.ajax({
// Either GET or POST will work.
type: 'GET',
data: 'format=json',
// Need to specify JSON data.
dataType: 'json',
url: $(this).attr('href'),
success: function(response){
// Call all callbacks.
alert('Success')
if (response.__callbacks) {
$.each(response.__callbacks, function(i, callback) {
// The first argument is a target element, the second
// the returned JSON data.
eval(callback)(target, response);
});
// If you don't want to return this module's own callback,
// you could of course just call it directly here.
// Drupal.AjaxLoadExample.formCallback(target, response);
}
}
});
Here is some example data that is returned by the call.
"\x3cdiv class=\"view view-Roster view-id-Roster view-display-id-page_1 view-dom-id-1\"\x3e\n \n \n \n \x3cdiv class=\"view-content\"\x3e\n \x3ctable class=\"views-table sticky-enabled cols-2\"\x3e\n \x3cthead\x3e\n \x3ctr\x3e\n \x3cth class=\"views-field views-field-picture\"\x3e\n \x3ca href=\"/drupal/roster?order=picture\x26amp;sort=asc\x26amp;format=json\" title=\"sort by Picture\" class=\"active\"\x3ePicture\x3c/a\x3e \x3c/th\x3e\n \x3cth class=\"views-field views-field-name\"\x3e\n \x3ca href=\"/drupal/roster?order=name\x26amp;sort=asc\x26amp;format=json\" title=\"sort by Name\" class=\"active\"\x3eName\x3c/a\x3e \x3c/th\x3e\n \x3cth class=\"views-field views-field-login\"\x3e\n \x3ca href=\"/drupal/roster?order=login\x26amp;sort=asc\x26amp;format=json\" title=\"sort by Last login\" class=\"active\"\x3eLast login\x3c/a\x3e \x3c/th\x3e\n \x3c/tr\x3e\n \x3c/thead\x3e\n \x3ctbody\x3e\n \x3ctr class=\"odd views-row-first\"\x3e\n \x3ctd class=\"views-field views-field-picture\"\x3e\n \x3cdiv class=\"picture\"\x3e\n \x3c/div\x3e \x3c/td\x3e\n \x3ctd class=\"views-field views-field-name\"\x3e\n admin \x3c/td\x3e\n \x3ctd class=\"views-field views-field-login\"\x3e\n \x3cem\x3e1 hour 5 min\x3c/em\x3e ago \x3c/td\x3e\n \x3c/tr\x3e\n \x3ctr class=\"even views-row-last\"\x3e\n \x3ctd class=\"views-field views-field-picture\"\x3e\n \x3cdiv class=\"picture\"\x3e\n \x3cimg src=\"http://www.mustangsquad.com/drupal/sites/default/files/images/profile/picture-3.jpg\" alt=\"Dägon\x26#039;s picture\" title=\"Dägon\x26#039;s picture\" /\x3e\x3c/div\x3e \x3c/td\x3e\n \x3ctd class=\"views-field views-field-name\"\x3e\n Dägon \x3c/td\x3e\n \x3ctd class=\"views-field views-field-login\"\x3e\n \x3cem\x3e3 hours 24 min\x3c/em\x3e ago \x3c/td\x3e\n \x3c/tr\x3e\n \x3c/tbody\x3e\n\x3c/table\x3e\n \x3c/div\x3e\n \n \n \n \n \n \n\x3c/div\x3e "
Any pointers or other assistance will be greatly appreciated. It will also keep my head from exploding. Thanks.
Comments
Comment #1
markus_petrux commentedYou may need to use drupal_json() on the server side to generate the response to the AJAX request.
For further assistance in using Ajax with Drupal, please look at the handbooks or ask in the development forums. Otherwise, such a request in this issues queue may end up unanswered for ages.