如何在html页面中从ajax返回json数据

how to return json data from ajax in html page

本文关键字:ajax 返回 json 数据 html      更新时间:2024-01-13

如何在html页面中返回json数据。我在js文件中有这段代码

$.ajax({
  url : 'auth.json',
  type: "GET",
  dataType : "jsonp",
 success: function(result) {
    $.each(result, function(i, v) {
        // For each record in the returned array
        $('#div_id').append(v.room_id); 
    });

index.html是

<!DOCTYPE html>
<html>
    <title>AjaxCall</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript" src="ajax.js"></script>
    <body>
        <div id="room_id">
        </div>
    </body>
</html>

我有json文件。但我不知道该怎么做。请帮帮我。谢谢!

您的代码看起来不完整。此外,您应该使用div ID"room_ID",而不是错误的"div_ID"。

$.ajax({
   url : 'path'auth.json',
   type: "GET",
   dataType : "json",
   success: function(result) {
      $.each(result, function(i, v) {
         $('#room_id').append(v.room_id); 
      });
   }
});