axios GET数据在div中显示为[undefined,undefined]

时间:2019-03-01 16:06:35

标签: javascript jquery json get axios

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

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

PS-不确定是否有帮助,但是我正在与此版本一起使用IE11(不是我想要的,但是对此我几乎没有发言权)。

index.js:

console.log

office.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 Data JSON:

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

    }

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

console.log screencap

1 个答案:

答案 0 :(得分:1)

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

response[0].office_number

response[0].floormap