Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么不是';我的JQuery get()函数是否适用于JSON URL,但是否适用于非JSON URL?_Javascript_Jquery_Json_Ajax_Get - Fatal编程技术网

Javascript 为什么不是';我的JQuery get()函数是否适用于JSON URL,但是否适用于非JSON URL?

Javascript 为什么不是';我的JQuery get()函数是否适用于JSON URL,但是否适用于非JSON URL?,javascript,jquery,json,ajax,get,Javascript,Jquery,Json,Ajax,Get,我的HTML文件和主Javascript文件在同一文件夹中有两个文件: hello.js hello.json 这是我的HTML: <!DOCTYPE html> <html> <head></head> <body> <script type="text/javascript" src="jquery-1.12.1.js"></script> <scrip

我的HTML文件和主Javascript文件在同一文件夹中有两个文件:

hello.js
hello.json
这是我的HTML:

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <script type="text/javascript" src="jquery-1.12.1.js"></script>
        <script type="text/javascript" src="page.js"></script>
    </body>
</html>
如您所见,这是一些相当复杂的代码。如上所示,Javascript可以很好地在控制台上打印“Hello”。当我将“hello.js”更改为“hello.json”时,不会打印任何内容。我尝试了几个不同的JSON文件,但没有任何效果


我认为这不重要,因为数据实际上没有被解析,但我确实验证了hello.JSON中的JSON数据格式是否正确。

尝试使用
$.getJSON()
而不是
$.get()
来检索JSON文件,或者在使用
$.get()检索JSON文件时尝试将数据类型指定为JSON
。这可能行得通。

也许您的服务器不提供json文件?你能给我们一个url来测试这一点吗?你在使用的任何浏览器上都检查过开发者工具中的网络面板了吗?(应该能够在大多数主流浏览器中使用F12打开它)。这应该告诉您是否发出了请求,如果是,服务器将返回什么响应。这将有助于缩小范围。这一点都不重要,
$。getJSON
只是
$的快捷方式。get
的数据类型设置为JSON。
$(document).ready(function() {
    $.get("hello.js", function(response) {
    console.log("Hello")
    });
});