0

I have a JSON file which contains HTML content. I want to load it in my main HTML file when a user clicks on a button.

ABC.json contains:

<li><img src="images/picture6.jpg" /></li>
<li><img src="images/picture5.jpg" /></li>
<li><img src="images/picture4.jpg" /></li>
<li><img src="images/picture3.jpg" /></li>
<li><img src="images/picture2.jpg" /></li>
<li><img src="images/picture1.jpg" /></li>

The Javascript code that I'm using is:

$("button").click(function(){
    $.getJSON("javascript/lib/domain.json", function(data){
        console.log(data);
    });
});

Unfortunately, it doesn't work.

2
  • 3
    if it contains just HTMLthen why dont you make it .html? Commented Jun 16, 2012 at 14:31
  • 7
    your json file is not json _ Commented Jun 16, 2012 at 14:32

3 Answers 3

5

The file ABC.json does not contain any valid JSON.

I think it's worthwhile visiting http://www.json.org/ You will get a better idea about json and how to use it.

Specifically you will find the way to pass html within json in different languages

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

1 Comment

Although I feel that the original poster, saorabh should visit this site to see what JSON actually consists of, I don't feel that this is a legitimate answer; you're just pointing him to a link. Instead, you should explain what he's doing wrong or why it can't be done (if that is the case).
2

Please edit your code:

abc.json

"img": [
        {"src": "images/picture6.jpg"},
        {"src": "images/picture5.jpg"},
        {"src": "images/picture4.jpg"},
        {"src": "images/picture3.jpg"},
        {"src": "images/picture2.jpg"},
        {"src": "images/picture1.jpg"},
       ]

Javascript

$("button").click(function(){
    $.getJSON("javascript/lib/abc.json", function(data){
        console.log(data);
    });
});

Comments

0

Your .json file contains pure html and not JSON. Why not use .html or .txt instead?

Or you can try this:

var my_html = $.parseJSON(data);
console.log(my_html);

2 Comments

First thanks Jhon.. Can you please guide me if I make it as a html file,then how I can achieve this.
If it is already an html file, then you can load it by using for example the following: $.load('list.html',function(data){ console.log(data); }); or $.get('list.html',function(data){ console.log(data); });

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.