javascript - axios GET 数据在 div 中显示为 [未定义, 未定义]

标签 javascript jquery json get axios

我正在尝试在浏览器中呈现网址的 JSON 数据,但它在 div 中显示为 undefined, undefined .当我将其响应放入console.log时对象及其数据出现,因此它出现在控制台和浏览器中之间存在某种脱节。我已经显示了其余的数据(其他 REST 调用),但在解决此问题之前,我将无法看到它们并继续我的项目。

对此有什么想法吗?一段时间以来,我一直在努力获取这些数据,这一直困扰着我。

PS - 不确定它是否有帮助,但我正在将 IE11 与此一起使用(不是我想要的,但我在这件事上没有什么发言权)。

index.js:

import axios from 'axios';

import officeComponent from './SiteAssets/scripts/office.js' // --- trying to put data here
import mattsComponent from './SiteAssets/scripts/matt.js'
import bioComponent from './SiteAssets/scripts/bio.js'

var queryDict = {};
location.search.substr(1).split("&").forEach(function(item) {
    queryDict[item.split("=")[0]] = item.split("=")[1]
});


axios.all([
    // Firm Directory
    axios.get(__[redacted].[redacted] + "/[redacted]/Odata/Odata.ashx/HSJS20FirmDirectory?hsf=@UserName=" + queryDict.uname + "&$select=PreferredName,...otherinfo...,SPID", {
        withCredentials: true,
        headers: {
        "Accept": "application/json; odata=verbose",
        "Content-Type": "application/json"
        }
    }),
... // ---------- other GET requests

]).then(axios.spread((firm, bio, edu) => { // params order is important (firmData, bioData, etc. must follow that order)

    let firmData = firm.data.d.results[0];
    let bioData = bio.data.d.results[0];

        // Office Info (relies on Firm Directory (firmData) SPID)
        axios.get(__[redacted].[redacted] + "/[redacted]/Odata/Odata.ashx/OFM_Resources?$select=FloorMap,FloorMapID,ResourceLocation,Office,OfficeID,Office_Number&hsf=@ResourceType=Person%20AND%20User_Link_ID=" + firmData.spid + "&hso=OfficeID", {
            withCredentials: true,
            headers: {
            "Accept": "application/json; odata=verbose",
            "Content-Type": "application/json"
            }
        })
        .then(function(response) {      
            let oComp = new officeComponent(response.data.d.results);
                oComp.loadOfficeData(response.data.d.results);  
                console.log('oComp', response.data.d.results); // --------- shows the object tree with all of the JSON data
        }).catch(function(err) {
            console.log(err);
        }),

// Matts Info (relies on Bio TK number)
        axios.get(__[redacted].[redacted] + "/[redacted]/Odata/Odata.ashx/MattsTeams?hsf=@MattStatus=active,pending%20AND%20TkNumber=%27" + bioData.number + "%27", {
            withCredentials: true,
            headers: {
            "Accept": "application/json; odata=verbose",
            "Content-Type": "application/json"
            }
        }).then(function(response) {      
            let mComp = new mattsComponent(response.data.d.results);
                mComp.loadMattsData(response.data.d.results);   
        }).catch(function(err) {
            console.log(err);
        })


let bComp = new bioComponent();
    bComp.loadBioData(bioData);
    bComp.loadEduData(eduData);

office.js:

import $ from 'jquery';
// ------------------- //
console.log("office.js working")
export default class {
    constructor() {

    }

    loadOfficeData(response) {
        $("#seat-val").append(response.office_number + ", " + response.floormap);  
    }   
}

Office 数据 JSON:

{
  "d": {
    "results": [
      {
        "floormap": "[location here]",
        "floormapid": 10,
        "resourcelocation": "[redacted]",
        "office": "[location here]",
        "officeid": 3,
        "office_number": "00-605"
      }
    ]
  }
}

console.log screencap

最佳答案

看起来结果是一个数组,因此您必须按以下方式访问它:

response[0].office_number

response[0].floormap

关于javascript - axios GET 数据在 div 中显示为 [未定义, 未定义],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54948312/

相关文章:

jquery - 使用 AJAX 或 jQuery 的密码长度检测器

java - Struts2 - 将重定向设置为 false

android - 使用 Volley 进行响应处理

php - Codeigniter/MYSQL 对每个州的城市进行分组

c++ - nlohmann json 从文件中读取

javascript - 如何使用 javascript 选择 <table> 的 <td>?

javascript - 无法在Leaflet map 上绘制从geoJson获取的点

javascript - 图像上的 Canvas

javascript - 如何使用 es6 展平嵌套对象数组

javascript - 检查在输入框中输入的值是否等于某个值