views:

57

answers:

3

I need to do a fetch using ajax. I need to use this: ajax/get_item_list/*group_id*/offset/*limit*/true (return true as JSON) where id comes from a link that user clicks. And when user clicks that link, it should call(?) that "ajax/get_item_list/*group_id*/offset/*limit*/tru" to get content to a div. And when user clicks another link (in navigation), it should do that again, but ofcourse it should get new content.

I am using drupal if that info is needed.

//Mario

A: 

Have you tried some jquery?

<div id="display"></div>
<a href="/ajax/get_item_list/1/1/50" class="ajaxToDisplay">Click me</a>

And then some javascript:

$(document).ready(function(){
    $('a.ajaxToDisplay').click(function(){
        $('#display').load(this.href);
        return false;
    });
});
Jage
That could work. Altought I think I can't have javascript in actual html markup. Everything would be in js file is better. But i will try this //Mario
mario
You can put that javascript in an external file and it will still work fine.
Jage
Ah sorry, i meant the part in href="..." It needs to bw href=#something" becauses it needed for other thing. Or can i put the /ajax/get_item... part to javascript file somehow aswell?
mario
I'm not sure I completely understand your question. If you mean can you put the url in the javascript file, you can. Just replace "this.href" from line 3 of the javascript with your url that you want to use or some function to return the url or whatever.
Jage
A: 

You can use JQuery.

$('a.link_class').click(function() {
  var group_id = $(this).href.replace(/.*#/, '');
  $.get("ajax/get_item_list/" + group_id + "/offset/" + limit "/true", null, function(data, status, xhr) {
   $('#your_div_id').html(data);
  });
});

and in html use links

link

jcubic
(data, status, xhr) what are the status and xhr? But thanks. Looks something that could work. Will try this also! -Mario
mario
xhr is XMLHttpRequest object (it's raw object to handle AJAX) and status is string ("success" when everything is ok)
jcubic
A: 

I get this kind of error on firebug: $(this).href is undefined [Break on this error] var group_id = $(this).href.replace(/.*#/, '');

this is when i use jcubic's proposal.

//mario

mario