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

458
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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