0
<!DOCTYPE html>
<html>
  <head>
  <title>test</title>
  <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
  </head>
  <body>
    <div id="div1">sqq</div>
    <div id="div2">dv2</div>

    <script>
    function getData(){
        $.ajax({
            type:"GET",
            url:"j.json",
            dataType:"json",
            success: function(jsondata){
                output(jsondata);               
            }
        });
    }

    function output(json){

        var Data = eval('(' + json + ')');
        var html = '';
        //alert(Data.length);
        for(var i=0;i<Data.length;i++){
            html += 'name' + Data[i].name + 'age' + Data[i].age;
        }

        document.getElementById('div1').innerHTML = html;
        document.getElementById('div2').innerHTML = Data[0].name;
    }

    setTimeout(getData, 3000);

    </script>             
  </body>
 </html>

Above is my code and json file content is:

    [{"name":"aaa","age":18},{"name":"bbb","age":19}]

I don't understand why this not working. I want to update the content of div1 using json data but nothing happened. Is it anything wrong when I tried to read the json data? I'm not familiar with dealing Json data, so please explain in detail. Thanks a lot.

4
  • 3
    What's not working? Do you have any errors in the console? What's that setTimeout() supposed to do? Wait for the ajax response? Commented Feb 25, 2014 at 20:38
  • See: stackoverflow.com/questions/4935632/… Don't use eval() for decoding JSON. Commented Feb 25, 2014 at 20:39
  • How is it not working? Commented Feb 25, 2014 at 20:40
  • Except for a few quirks, like using eval(), it looks fine! You have to tell us exactly what isn't working? You have set the dataType to JSON, so you should already have an object, and passing that to eval() would probably fail, but you did of course open the console and check for errors, as that what surely show up ? Commented Feb 25, 2014 at 20:42

1 Answer 1

3

You don't have to use 'eval'. You will already receive json object in 'jsondata' var.

function output(json){


        var html = '';

        for(var i=0;i<json.length;i++){
            html += 'name' + json[i].name + 'age' + json[i].age;
        }

        document.getElementById('div1').innerHTML = html;
        document.getElementById('div2').innerHTML = json[0].name;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

This works. Thanks so much. But in which occasion shall I use eval to parse json string to object?
You are telling jquery, that you expect json result by setting dataType=json. If you don't specify data type, then jquery will try to figure out response data type from response header. If server response header doesn't match json content type (it's set like text/plain or something), then you can use function that parses string to json.
If I don't use 'dataType:"json",', what's the default datatype?

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.