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

191
Visualizações
JavaScript to use a different proxy link if the first one returns a 404 error

I have some JS that works as follows: If the Word Count button is pressed, go to the Word Count function, if the status of the requested page is 200, then let the url be proxyURL, which is defined correctly in index.html and this as it stands works.

I have then added an else if statement to say if a 404 is returned, then go to the function "WordCountProxyBackup" function, which works the same way as the original, but instead of using "proxyURL", it uses proxybackupURL, which is defined in index.html

I have intentionally broken by original proxyURL to try and test this, and a 404 is returned, but the button is not finding it's way to the backup function to then find it's way to the backup link, can someone help with this? The code is below.

   function Wordcount()
{
    $(".operation").attr('disabled', true);
    this.disabled = true;
    let xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var j = JSON.parse(this.response);
            document.getElementById('output').value = j.answer;
        }else if (this.readyState == 4 && this.status == 404) {
            document.getElementById('output').value = "Error Bad Pathway - Rerouting";
            WordcountProxyBackup();
        }
    };

    let url = proxyURL + "/?text=" + encodeURI(document.getElementById('content').value) + "&route=" + "wordcount";
    xhttp.open("GET",url);
    xhttp.send();
    $(".operation").attr('disabled', false);
}

function WordcountProxyBackup()
{
    $(".operation").attr('disabled', true);
    this.disabled = true;
    let xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var j = JSON.parse(this.response);
            document.getElementById('output').value = j.answer;
        }else if (this.readyState == 4 && this.status == 500) {
            document.getElementById('output').value = "Error Bad Pathway - Rerouting";
            WordcountProxyBackup2();
        }
    };

    let url = proxybackupURL + "/?text=" + encodeURI(document.getElementById('content').value) + "&route=" + "wordcount";
    xhttp.open("GET",url);
    xhttp.send();
    $(".operation").attr('disabled', false);
}
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