Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
URL in Ajax open()

This is my first Ajax program and I can't fix the code because I'm not sure where/what the problem is.

The error(which I'm unable to interpret) while using the debugger is, XMLHttpRequest cannot load http://localhost/function.txt. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

function calling()
{
    var x;
    if (window.XMLHttpRequest) {
    x = new XMLHttpRequest();
    } else {

    x = new ActiveXObject("Microsoft.XMLHTTP");
    }

    x.onreadystatechange = function(){
        if(this.readyState == 4 && this.status == 200)
        {
            document.getElementById("block").innerHTML = this.responseText;
        }

    };
    x.open("GET", "http://localhost/function.txt",true);
    x.send();
}

function.txt

<html>
<head></head>
<body>
<h2>Ajax is working</h2>
</body>
</html>
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Is your js located at the same location as your function.txt?

For more information about CORS, have a look at this link: https://www.html5rocks.com/en/tutorials/cors/

UPDATE: This works for me, I think there is maybe something with your Apache settings...

function calling()
{
  var xhr = new XMLHttpRequest(),
      method = "GET",
      url = "function.txt";

  xhr.open(method, url, true);
  xhr.onreadystatechange = function () {
          if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
              alert(xhr.responseText);
          }
      };
  xhr.send();
}

calling();
over 4 years ago · Santiago Trujillo Denunciar

0

You cannot make Ajax calls to a url from a different domain if said domain does not explicitly allow it (via 'Access-Control-Allow-Origin' header). Your error means that you're making your Ajax call from another domain. If your function.txt file is located at the same location as your js, try using relative path in your .open().

over 4 years ago · Santiago Trujillo Denunciar

0

You are attempting a CORS request, which is unsafe and is prohibited by browsers by default. If you are in control of the target site, you can enable CORS. If that's not the case, then you will need to write a page which will be used as a proxy, that is, you will send the request to this page instead of the target site's page. The page, on its turn will send the request to the target page and send the output to the browser. While this is a workable solution you will need to make sure that all the absolute paths of the target site are handled well.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda