You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从接口返回JSON中提取imdbID字段并赋值给对应id的div元素

问题原因

  • 字段层级错误:你要取的imdbID字段不在返回数据的根层级,而是存放在Search数组的第一个元素内
  • DOM赋值属性错误:div元素没有value属性,value仅适用于input、textarea等表单元素,给div设置文本内容需要用innerText属性

修正后代码

const settings = {
  async: true,
  crossDomain: true,
  url: 'https://movie-database-imdb-alternative.p.rapidapi.com/?s=' + res + '&r=json&y=[xfvalue_jahre]&page=1',
  method: 'GET',
  headers: {
    'x-rapidapi-host': 'movie-database-imdb-alternative.p.rapidapi.com',
    'x-rapidapi-key': '137f269ad5m5jsn65ff6dcb7b43',
  },
};

$.ajax(settings).done(function (data) {
  // 增加校验避免无匹配结果时报错
  if(data.Response === 'True' && data.Search.length > 0) {
    const targetImdbId = data.Search[0].imdbID;
    console.log(targetImdbId);
    document.getElementById('imdbid').innerText = targetImdbId;
  }
});

内容的提问来源于stack exchange,提问作者Alexander Schulz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 14:24:04