1

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>

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

This question came from our site for professional and enthusiast programmers.

  • 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
2
$.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
    }
  }
})
  • 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
0

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.

0

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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