Ega Setya Putra
Ega Setya Putra

Reputation: 1695

JQuery AJAX-How to retrieve JSON data from URL?

I'm working on some code that need to retrieve data from API. The idea is get json data and decode it when I have the data, I've done this before using file_get_contents and json_decode on PHP.

For the detail:

I need that code for seat reservation that user could choose their own seat.So I made the seat map with table and the "td" is clickable. It all works fine except for the API thing. What I want is when the seat already clicked/chosen I retrieve data from API.

I've tried:

$.getJSON(jos, function(jd) {
    var hah = $.parseJSON(jd);
    alert(jd);
});

$.ajax({
    url: jos,
})
.done(function(data) {
    alert('data');
});

note: jos is variable that contains url for my API

I'll appreciate any response

Upvotes: 1

Views: 2245

Answers (2)

user3308224
user3308224

Reputation: 423

$.ajax(
   url: jos,
   dataType: "json",
   success: function(data){
      alert(data);
   }

);

Specifying dataType as json means that the you are expecting a json object from the server.

Upvotes: 0

low_rents
low_rents

Reputation: 4481

you don't need to convert the data from $.getJSON() to JSON, since it already is a JSON string:

$.getJSON(jos, function(jd) {
    alert(jd);
});

Upvotes: 1

Related Questions