-5
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
  <script src= "C:\Documents and Settings\vivek.aggarwal\My Documents\Downloads\jquery-1.11.1.js"></script>
  <script src="mydata.json"></script>
</head>
<body>
    <p id="demo"> ABC </p>
    <script>

    $.getJSON(mydata.json, function(data) {
        var output = data.login[0].one;
        document.getElementById("demo").innerHTML= output;
    });

    </script>
</body>

and this is my JSON file (mydata.json)

{
    "login": [
        {
            "one": "vivek"
        }
    ]
}
4
  • 1
    I'd start by quoting the URI, ie $.getJSON('mydata.json', .... Of course, your console would have alerted you to this error, if you'd bothered to check it Commented Jul 25, 2014 at 5:46
  • 1
    Get rid of the duplicate jQuery script tags (c:\documents....) and change $.getJSON('mydata.json'...) Note the quotes. Please learn to use developer toolbar (press F12 in Chrome) Commented Jul 25, 2014 at 5:46
  • ... also, don't include mydata.json via a <script> tag Commented Jul 25, 2014 at 5:47
  • and while you're using jQuery anyway... $('#demo').html(data.login[0].one) Commented Jul 25, 2014 at 5:49

2 Answers 2

1

Try this method, it works for me.

$.ajax({
    url: 'mydata.json',
    dataType: 'json',
    success: function(result) {
       //Your code here, result is json obj
       $('#demo').html(result.login[0].one);
    },
    error: function() {
      // alert("error");
    }
});
Sign up to request clarification or add additional context in comments.

8 Comments

Thnx Phil, i replaced with jQuery for assignment.
What's the reason for steering away from $.getJSON?
For short answer to steering away, .getjson() calls .ajax().
Yes, I know they're equivalent. That's why I asked
|
0

Try this it should work.

$.getJSON("mydata.json", function(data) { 
        var output = data.login[0].one;
        document.getElementById("demo").innerHTML= output;
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.