Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
XMLHttpRequest() shows object in console.log(), but object properties are inaccessible

I'm trying to write a script that activates when an external URL is inaccessible, and refreshes the page when the external URL becomes reachable again.

The problem is the object created by XMLHttpRequest() seems like its various properties are inaccessible.

Here's the code in play:

request.open('GET', 'https://www.url-to-monitor.com', true);
request.send();
console.log(request.status);
console.log(request);
console.log(Object.keys(request));

Pretty simple stuff there — the expectation is that when the connection fails, request.status will be 0, and when the connection succeeds, request.status will be 200.

But here's the output when a connection is successful:

0

XMLHttpRequest {onreadystatechange: null, readyState: 1, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
    onabort: null
    onerror: null
    onload: null
    onloadend: null
    onloadstart: null
    onprogress: null
    onreadystatechange: null
    ontimeout: null
    readyState: 4
    response: "0{\"sid\":\"dYHKY5PCHemvcbgPAAAA\",\"upgrades\":[\"websocket\"],\"pingInterval\":25000,\"pingTimeout\":20000}"
    responseText: "0{\"sid\":\"dYHKY5PCHemvcbgPAAAA\",\"upgrades\":[\"websocket\"],\"pingInterval\":25000,\"pingTimeout\":20000}"
    responseType: ""
    responseURL: "<mydomain>/socket.io/?EIO=4&transport=polling"
    responseXML: null
    status: 200
    statusText: "OK"
timeout: 0
upload: XMLHttpRequestUpload {onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, …}
    withCredentials: false
    [[Prototype]]: XMLHttpRequest

[]
    length: 0
    [[Prototype]]: Array(0)

So right at the top that zero should be 200. It's the output from console.log(response.status), and according to the output from console.log(response) we should be able to assume that response.status is 200, not zero.

Then at the bottom, Object.keys(response) outputs an empty array again, indicating that there's no such thing as response.status, or any of the other object properties that console.log(response) tells us exist.

From some googling and reading other threads I can surmise that the inconsistency with Object.keys() is due to XMLHttpRequest keeping its object properties in the prototype, but I can't find anything that explains how to actually access those property values.

I've tried console.log(request.proto.status) but instead of outputting "200" as expected, it throws an "Illegal invocation" error.

So TL;DR — how can I access the value of response.status?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda