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

445
Views
jQuery ajax request from local file (Origin 'null' not allowed access)

I am making a jQuery ajax call from a local file which retrieves JSON data from a webserver:

$.ajax({
    type: 'GET',
    url: 'http://mywebsite.com/data.json',
    dataType: 'json',
    success: showData
});

and I am getting the following error on chrome:

XMLHttpRequest cannot load http://mywebsite.com/data.json. No 
'Access-Control-Allow-Origin' header is present on the requested 
resource. Origin 'null' is therefore not allowed access.

I have read through a lot of other answers which seem to come to the conclusion that you cannot make ajax requests from local files with chrome, so the solutions are:

  • Host the file making the request on the sever
  • Run chrome with the "--allow-file-access-from-files" flag

but neither of these options are suitable for me.

However, when I host the JSON data on another site (myjson.com), so that the line in the ajax request reads

url: 'https://api.myjson.com/bins/myfile'

the file loads perfectly and the data is correctly displayed.

Why is the ajax request allowed to 'myjson.com' but not to 'mywebsite.com'?

Edit:

I have the line

Header set Access-Control-Allow-Origin "*"

in my apache2.conf file

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

What you need to do is create a .htaccess file in your document root and add the following lines to it:

<FilesMatch "\.(json)$">
    Header set Access-Control-Allow-Origin "*"
</FilesMatch>

What the above lines do is, they allow Cross origin resource sharing for all domains on .json files thus removing the error.

over 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!