2

I have a problem with creating menu using UL LI List and JQuery with JSON. Here is my JFiddle:

http://jsfiddle.net/TqcEs/

    $.getJSON('http://www.speedyshare.com/zr6HF/download/doc22.json', function (data) {

    //$("body div#content").html(makeUL(data.menu[0]));
    $("body .menu ").html(makeUL(data.menu));

});
//$("body div#content").html("</li></ul>");

function makeUL(lst) {
    var html = [];
    //html.push("<div class=sub"+count+">");
    html.push("<ul>");
    count++;
    $(lst).each(function () {
        html.push(makeLI(this));
    });
    html.push("</ul>");
    //html.push("</div>");
    return html.join("\n");

}

function makeLI(elem) {
    var html = [];

    html.push("<li >");

    //html.push(elem.name);
    if (elem.link)
    //html.push("<div class=item"+count2+">");
    html.push("<a>" + elem.link + "</a>");
    //html.push("</div>");
    count2++;

    if (elem.sub) html.push(makeUL(elem.sub));
    html.push("</li>");
        //
        return html.join("\n");

}

In $.getJSON currently is direct link to JSON file, but he isn't working in JSFiddle(I don't know why), with external file on hard disk , works good.

Ok, I would like to do similar menu such as in this JSFiddle:

http://jsfiddle.net/65R8q/31/ ,but with data from my JSON file. I tried around div's in many ways, but still not working. :-/

someone know where I do make mistake?

1

1 Answer 1

1

As adeneo commented, it's due to Same Origin Policy. You need to use JSONP when making the Ajax code. Add callback=? parameter to the calling URL:

$.getJSON('http://www.speedyshare.com/zr6HF/download/doc22.json?callback=?', function (data) {

    //$("body div#content").html(makeUL(data.menu[0]));
    $("body .menu ").html(makeUL(data.menu));

});

http://jsfiddle.net/TqcEs/1/

But the page you are requesting is HTML, not JSON data so a syntax/JavaScript error thrown.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.