Jquery $ .get是不可接受的

coo*_*guy 1 javascript jquery json

我有这个标记和一个json.txt文件位于同一目录.im无法获取它的内容..所以我没有在我的firebug中得到任何错误

<!DOCTYPE html>
<html>
    <head>
     <script type="text/javascript" src="jquery.js"></script>   
     <script>
     $(document).ready(function(){ 
       $.get('/json.txt', 
       function(data) { 
            $('div.result').html(data);

        });
     });
     </script>
    </head>

    <body>
    <div class="result"></div>
    </body>         
    </html>
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 5

"在同一页上" ?你的意思是"在同一个目录中"?如果是这样的话

$.get('json.txt'
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,我建议使用长形式来看看会发生什么:

$.ajax({
  url: 'json.txt',
  success: function(data){console.log(data)},
  error: function(jqXHR, textStatus, errorThrown) {console.log(jqXHR, textStatus, errorThrown)};
});
Run Code Online (Sandbox Code Playgroud)

所以你可以在你的控制台(ctrl + maj + i)中看到错误(或数据).

另一个注意事项:如果您打开html文件,这将无法工作,file://因为json将被视为来自另一个域.您必须有一个http服务器并将其打开http://.