首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在浏览器中打印json console.log数据?

如何在浏览器中打印json console.log数据?
EN

Stack Overflow用户
提问于 2017-11-18 04:51:51
回答 2查看 1.5K关注 0票数 1

我试图使用javascript在浏览器中输出Json数据,但我只能在console.log中输出,我不知道该搜索什么。我是javascript的初学者,请在这里帮助我。

script.js

代码语言:javascript
复制
$(document).ready(function() {
    var url = 'http://api.themoviedb.org/3/',
        mode = 'movie/',
        movie_id,
        key = '?api_key=e9dfeccf734a61b9a52d9d7660f0d0a1';

    $('button').click(function() {
        var input = $('#movie').val(),
            movie_id = encodeURI(input);
        $.ajax({
            type: 'GET',
            url: url + mode + movie_id + key,
            async: false,
            jsonpCallback: 'testing',
            contentType: 'application/json',
            dataType: 'jsonp',
            success: function(json) {
                console.dir(json);
            },
            error: function(e) {
                console.log(e.message);
            }
        });
    });
});

index.php

代码语言:javascript
复制
<input id="movie" type="text" /><button>Search</button>

这个代码输出console.log中的所有数据,但我想做的是它应该在浏览器中显示数据,我想输出一些特定的对象,比如电影标题、概览和图像。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-18 05:14:53

使用键检索特定值并显示它。对象具有键和值。执行object.key将给value

代码语言:javascript
复制
$(document).ready(function() {
  var url = 'https://api.themoviedb.org/3/',
    mode = 'movie/',
    movie_id,
    key = '?api_key=e9dfeccf734a61b9a52d9d7660f0d0a1';

  $('button').click(function() {
    var input = $('#movie').val(),
      movie_id = encodeURI(input);
    $.ajax({
      type: 'GET',
      url: url + mode + movie_id + key,
      async: false,
      jsonpCallback: 'testing',
      contentType: 'application/json',
      dataType: 'jsonp',
      success: function(json) {
        $("#title").text(json.title);
        //$("#movTitle").prop('src'); // add image path here
        $("#overview").text(json.overview) //overview is a key
      },
      error: function(e) {
        console.log(e.message);
      }
    });
  });
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="movie" type="text" /><button>Search</button>
<!-- Search This: 346364 -->
<div id="title">
</div>
<div>
  <img id="movTitle" src="" alt="">
</div>
<div id="overview">

</div>

票数 0
EN

Stack Overflow用户

发布于 2017-11-18 05:16:58

要将JSON数据输出到浏览器,需要修改页面的HTML。

首先,将几个元素添加到index.php中,如下所示:

index.php

代码语言:javascript
复制
<input id="movie" type="text" /><button>Search</button>

<h1>Movie info:</h1>
<p>Movie title: <span id="movie-title"></span> </p>
<p>Movie budget: <span id="movie-budget"></span> </p>

然后,在您在jQuery ajax请求中定义的成功回调中,您可以抓取span元素并使用jQuery的text函数替换它们的文本,如下所示:

代码语言:javascript
复制
$(document).ready(function() {
    var url = 'http://api.themoviedb.org/3/',
    mode = 'movie/',
    movie_id,
    key = '?api_key=e9dfeccf734a61b9a52d9d7660f0d0a1';

    $('button').click(function() {
        var input = $('#movie').val(),
        movie_id = encodeURI(input);
        $.ajax({
            type: 'GET',
            url: url + mode + movie_id + key,
            async: false,
            jsonpCallback: 'testing',
            contentType: 'application/json',
            dataType: 'jsonp',

            success: function(json) {

                // grab the span elements by ID and replace their text with the json text

                $("#movie-title").text(json.title);
                $("#movie-budget").text(json.budget);

                console.dir(json);
            },

            error: function(e) {
                console.log(e.message);
            }
        });
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47362633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档