2

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>
2
  • is ajax switched on in settings of page_1 ?
    – dobeerman
    Commented Jun 14, 2011 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
    Commented Jun 14, 2011 at 22:44

3 Answers 3

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
    }
  }
})
3
  • 1
    Does that code work on Drupal 6 too?
    – apaderno
    Commented Dec 12, 2012 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
    Commented Dec 18, 2012 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.
    – apaderno
    Commented Dec 18, 2012 at 11:10
1

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

4
  • Here are some screens of Firebug-- link link Thanks, I'll take a look at those links. It's a D6 install.
    – allanb
    Commented Jun 14, 2011 at 22:53
  • Ah, of course. How would I use the 'display' property? data.property?
    – allanb
    Commented Jun 15, 2011 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. Commented Jun 15, 2011 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
    Commented Jun 15, 2011 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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