tags:

views:

12

answers:

1

How do i display the values that JSON returns?


function akaiphoneResponse(searchText){        
    $.ajax({
        type: "GET",
        url: Drupal.settings.basePath + 'akaiphone/response',
        data: 'search_text=' + searchText,
        success: function(data) {
      $("#search-results", this).empty(); 
      var jsob = jQuery.parseJSON(data.d);
      $.each(data, function(i, item){
        content = item.link + "-" + item.title ;

          $(content).appendTo("#search-results");

      });
        }
    });
}

JSON:


{"matches":[{"title":"JUR 3420 Forretningsjus","link":"6451"}]}

atm title is always gives "undefined", and link turns out "function link() {[native code]}" im a total noob at ajax and javascript, any help would be appreciated!

A: 

Try this as a replacement of your each function:

$.each(jsob.matches, function(i, item){
    content = item.link + "-" + item.title ;
    $(content).appendTo("#search-results");
});

Working example: http://jsfiddle.net/kGnJ2/

Fosco
i get a "i is undefined" error if i do that :/
Thor
@Thor check the JSFiddle link I just added.. where is that error coming up?
Fosco
it was on the last line, but i still had data in the code: $.each(DATA.matches, function(i, item), i changed it to jsob now and got a different error: jsob is null
Thor
@Thor in your question, there is this line right before the each function: var jsob = jQuery.parseJSON(data.d);
Fosco
sweet, works now, but only returns 1 search result, must be my php
Thor