Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
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>
about 4 years ago · Santiago Trujillo
3 answers
Answer question

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();
about 4 years ago · Santiago Trujillo Report

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().

about 4 years ago · Santiago Trujillo Report

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!