3

I'm using jquery ui tabs with ajax.

Ajax will face a JSON content like this.

[
  {
    "title"   :"a note",
    "type"    :"text",
    "content" :"MY FIRST NOTE!"
  },
  {
    "title"   :"two note",
    "type"    :"text",
    "content" :"MY FIRST NOTE <b>if html works<\/b> i should pay attention to this!"
  }
]

I'm using this code:

$(function() {
    $("#tabs").tabs({
        cache : false,
        ajaxOptions : {
            cache : false,
            dataType : 'json',
            dataFilter : function(result) {
                var data = $.parseJSON(result);
                return data;
            },
            error : function(xhr, status, index, anchor) {
                $(" anchor.hash ").html("Couldn't load this tab. We'll try to fix this as soon as possible. " + "If this wouldn't be a demo.");
            }
        }
    });
});

(i've seen this question Loading JSON-encoded AJAX content into jQuery UI tabs)

The JSON file (is generated by php) is correctly loaded and I have validated it using JSONLint but the tab remain white and the content isn't loaded, can you help me?

It's the first time that i work with JSON and Ajax so forgive me if I'm doing some stupid error

EDIT: the json content is sent with a content type = application/json, removing the content type it display the json but i want to parse the json file using jquery is that possible?

Community
  • 1
  • 1
Matteo Pagliazzi
  • 5,140
  • 12
  • 49
  • 83

3 Answers3

5

I think you should not call $.parseJSON(result); since you specified dataType : 'json' (look at my answer to this question Why is 'jQuery.parseJSON' not necessary? ) and so jQuery parses the response for you. Looking at the other example you should also return

       dataFilter : function(result) {
            var data = $.parseJSON(result);
            return data.content;
        },

EDIT - letting dataType: 'json' this should be ok

       dataFilter : function(result) {
            return result.content;
        },
Community
  • 1
  • 1
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
2

The content isn't properly loaded to the tab because of this part:

dataType : 'json',
dataFilter : function(result) {
    var data = $.parseJSON(result);
    return data;
}

You receive data as json, so result is javascript object. But $.parseJSON() requires string, not object (see docs). It works - as you write - when dataType : 'html' because with this setting data is returned as text.

To fix it you can either set dataType : 'html' or get rid of dataFilter part. When you ask for data in json format jQuery calls $.parseJSON() internally.

UPDATE: One more information why you don't get any errors with $.parseJSON() - As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently see docs too

BartekR
  • 3,827
  • 3
  • 24
  • 33
0

I found it simpler to return false to the beforeLoad handler and send a getJSON request from within the beforeLoad handler. See my answer here.

Community
  • 1
  • 1
DavidHyogo
  • 2,838
  • 4
  • 31
  • 48