简体   繁体   English

jQuery AJAX-如何从URL检索JSON数据?

[英]JQuery AJAX-How to retrieve JSON data from URL?

I'm working on some code that need to retrieve data from API. 我正在处理一些需要从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. 我的想法是获取json数据并在有数据时对其进行解码,在PHP上使用file_get_contents和json_decode之前,我已经做到了。

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. 我需要该代码来预订座位,以便用户可以选择自己的座位。因此,我使用表格制作了座位图,并且可以点击“ td”。 It all works fine except for the API thing. 除了API以外,其他一切都正常。 What I want is when the seat already clicked/chosen I retrieve data from API. 我想要的是当座位已经被点击/选择时,我从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 注意:jos是包含我的API网址的变量

I'll appreciate any response 我将不胜感激

you don't need to convert the data from $.getJSON() to JSON, since it already is a JSON string: 您不需要将数据从$.getJSON()转换为JSON,因为它已经是JSON字符串:

$.getJSON(jos, function(jd) {
    alert(jd);
});
$.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. 将dataType指定为json意味着您期望服务器提供json对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM