2013-07-30 19 views
10

Ho creato un semplice file html con ajax semplice.Ajax in Jquery non funziona dal file locale

index.html:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; Charset=UTF-8"> 
<script type="text/javascript" src="jquery.js"></script> 
</head> 
<body> 
    <div id="content"></div> 

    <script> 
     function show() 
     { 
       $.ajax({ 
       url: "2.html", 
       cache: false, 
       success: function(html){ 
        $("#content").html(html); 
       } 
      }); 
     } 

     $(document).ready(function(){ 
      show(); 
      setInterval('show()',1000); 
     }); 
    </script> 

</body> 
</html> 

File 2.html si trova nella stessa directory del file index.html . E contiene ad esempio:

<p>ssss hkl jh lkh <b>d1111</b></p> 

Quando eseguo il index.html sul server web, tutto funziona. Ma se si esegue il file index.html sul computer come un file locale, ajax non funziona. Come sistemarlo?

+0

è 'jquery.js' incluso accanto al' index.html'? –

+3

Sei in Chrome? Chrome non ammette materiale ajax locale. – Jack

risposta

10

Questo è un problema noto con Chrome, se si sta verificando. Usa XAMPP per eseguire un server web locale e prova la tua chiamata ajax.

Controllare questo biglietto: https://code.google.com/p/chromium/issues/detail?id=40787

+4

Ancora più veloce, usa Python (ovviamente). Nel terminale: 'cd/path/to/progetto/folder' poi '> python -m SimpleHTTPServer 8080' Utilizzare http://127.0.0.1:8080/ o una variante il sistema potrebbe desiderare. (OSX, Py 2.7x) –

+2

Oppure potresti usare node.js. > npm install -g http-server > cd/percorso/su/progetto/cartella > http-server – Mark

15

Alcuni browser implementano forti misure di sicurezza per impedire alle pagine Web scaricate di accedere a file arbitrari sul file system.

Passare a un browser con sicurezza più debole (penso che Firefox consenta l'accesso ai file locali tramite XHR) o interrompere il tentativo di eseguire un sito Web senza HTTP.

+0

Anche con chrome con la seguente riga di comando: 'chrome --allow-file-access-from-files' – pdem

Problemi correlati