Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON from URL

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?

like image 866
uto Avatar asked Feb 04 '12 11:02

uto


2 Answers

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.

like image 69
Christofer Eliasson Avatar answered Sep 23 '22 14:09

Christofer Eliasson


$.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

like image 31
Ashish Agarwal Avatar answered Sep 23 '22 14:09

Ashish Agarwal