Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm trying to return the view data and display it in a list for verification.

It is sending for the data and seems to receive it (I get a 200), but it is not outputting the data.

Any help much appreciated!

<div id="ajax"></div>

<script type="text/javascript">
$(document).ready(function(){

    $.getJSON('/views/ajax?view_name=latest_new&view_display_id=page_1&view_args=city', function(data) {
        var items = [];

        $.each(data.items, function(i, item) {
            items.push('<li id="' + i + '">' + item + '</li>');
        });

        $('<ul/>', {
            'class': 'my-new-list',
            html: items.join('')
        }).appendTo('#ajax');

    });

});
</script>
share|improve this question
is ajax switched on in settings of page_1 ? – dobeerman Jun 14 '11 at 0:51
It wasn't because I didn't want to use an Ajax pager for the view. I switched it on and I still get the same result. In the Firebug Console tab I can see the request and it returns a '200 OK' as well as data (I can view it on the Response & JSON sub-tabs of the Console tab) but I don't get anything outputted on screen. – allanb Jun 14 '11 at 22:44

migrated from stackoverflow.com Jun 17 '11 at 18:01

3 Answers

$.ajax({
  url:'views/ajax',
  dataType: 'json',
  data:{
    view_name: 'my view name',
    view_display_id: 'default',
    view_args: 'view argument goes here',
  },
  success: function(data, textStatus, jqXHR){
    if(data[1].data !== undefined){
      // the view results will be in data[1].data
    }
  }
})
share|improve this answer
1  
Does that code work on Drupal 6 too? – kiamlaluno Dec 12 '12 at 4:04
it's jQuery so it should. Though I forget what version of jQuery Drupal 6 has. I think there is a module called 'jquery update' to update the jquery version easily in drupal 6. – 2pha Dec 18 '12 at 10:50
I was wondering if the menu callback for views/ajax would accept parameters that are different between Drupal 6, and Drupal 7. Apparently, there isn't any difference. – kiamlaluno Dec 18 '12 at 11:10

I don't think /views/ajax's result is as you expect it to be. On Drupal 7, it returns an array of Ajax Commands, not the views results as an array of strings. See views_ajax() (or views_ajax() for Drupal 6).

If you want to retrieve the results of a views as a JSON array, it's probably easier to use the Views Datasource module.

After viewing the Firebug screenshot, there is no items properties on the dataargument of the $.getJSON's callback. Your $.each(data.items, function(i, item) {...}); loops over an undefined value and does nothing. The response to your $.getJSON request contains a displayproperty which seems to be the rendered results from the view. Again, I don't thing that the data your are looking for and you need something to get the results as structured JSON data.

share|improve this answer
Here are some screens of Firebug-- link link Thanks, I'll take a look at those links. It's a D6 install. – allanb Jun 14 '11 at 22:53
Ah, of course. How would I use the 'display' property? data.property? – allanb Jun 15 '11 at 13:42
I guess everybody has to start somewhere. You should really figure what technologies you are using and learn them before trying to do anything with it. For JavaScript and JSON, see developer.mozilla.org/en/JavaScript/Guide json.org/js.html. For Drupal, see drupal.org/getting-started, drupal.org/documentation/develop. For PHP, see php.net/manual/en/tutorial.php. – Pierre Buyle Jun 15 '11 at 17:35
Thanks. I know PHP and JS/jQuery, but nothing advanced. I'm self taught so I may have missed some fundamentals. I guess I better get studying again. – allanb Jun 15 '11 at 18:54
up vote 0 down vote accepted

For anyone interested in my (current) solution.

My view outputs all nodes date descending. My secondary menu consists of taxonomy terms that can be passed to the view as arguments. The code below works for displaying nodes containing the specified taxonomy term.

It needs work, but so far so good.

$('#block-menu-secondary-links a').bind('click', function(){
    var arg = $(this).attr("title");
    $.get('views/ajax?view_name=latest_new&view_display_id=page_1&view_args='+arg, null, response);
    return false;
});

var response = function(data) {
    var result = Drupal.parseJson(data);
    $('#content').html(result.display);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.