5

I am building a web site and I use a url which returns a JSON response, like:

{name:mark; status:ok}

I would like to obtain the name, using only JavaScript or jQuery in my HTML page.

Can somebody help me to do this?

1
  • 8
    Your JSON is bogus. Other than that, did you try searching for "jquery json"? Commented Feb 4, 2012 at 11:57

3 Answers 3

7

Have a look at jQuery's .getJSON() method, which will make it really easy for you.

$.getJSON('yourURL.php', function(data) {
  alert(data.name);
});

If you need more flexibility you should have a look at .ajax() instead. .getJSON() is really just a short hand for the .ajax() method, suitable for making simple requests to fetch JSON. With .ajax() you will have a lot of more options - specifying an error handler for instance, and much more.

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

Comments

4
$.getJSON("URL", function(json) {
   alert("JSON Data: " + json.name);
 });

I guess this will work for you.

If you want to Pass parameters then here is code

$.getJSON("URL", { name: "John", time: "2pm" }, function(json) {
    alert("JSON Data: " + json.name);
    });

Refer link

2 Comments

I tried it...in my html page I put it but it doesn't work...;(!!! I put in my page this <script src="code.jquery.com/jquery-latest.js"></script>...but nothing!!!
it would be great if you can add more code for better understanding.
0
   $(document).ready(function () {
    var url = 'https://graph.facebook.com/me';
    $.getJSON(url, function (data) {
        alert(data.name);
    });
}); 

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.