繁体   English   中英

JQuery 问题“TypeError:$.getJSON 不是函数”

[英]JQuery issue "TypeError: $.getJSON is not a function"

我有这段代码:

$(document).ready(function () {
    $.getJSON('http://localhost:5000/', function (response) {
        console.log(response);
    });
});

localhost:5000 是一个flask/python脚本,它返回一个json ,如:

{
  "data": [
    0, 
    0, 
    0, 

我得到:

$.getJSON is not a function TypeError: $.getJSON is not a function

有什么技巧可以让我开始解开排球吗?

谢谢!

编辑:

HTML:

<!DOCTYPE html>
<html>

<head>
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
    <script src="lib/main.js"></script>
</head>

<body>
</body>

</html>

lib/main.jsdocument.ready所在的位置。

谢谢!

您似乎使用的是slimjquery版本,它没有getJSON方法,这就是您收到此错误的原因。

请使用以下链接中的完整版jquery代替。

https://code.jquery.com/jquery-3.1.1.min.js

Slimjquery不包括ajaxanimations effects

因此,在将代码库从 jQuery v1.x+ 迁移到 v3.0+ 时经常发生这种情况,因为 jQuery 更新/弃用/停止了它的一些 API。

我建议使用 jQuery Migrate 来解决这个问题以及其他问题:

通过 CDN 在此处获取:

https://cdnjs.com/libraries/jquery-migrate

如果使用 Gulp/Grunt,您可以使用导入到您的项目中

npm install --save jquery jquery-migrate

Github 存储库 - https://github.com/jquery/jquery-migrate

阅读更多关于 jQuery v3.0+.. http://blog.jquery.com/2016/06/09/jquery-3-0-final-released/

function cinta(){
$.getJSON('http://localhost:5000/', function (response) {
        console.log(response);
    });
}
cinta();
$(document).ready(function () {
  console.log('yesss');  
});

在 python 烧瓶中为我工作

您还可以将Fetchasyncawait一起使用:

async function getData(){
  const response = await fetch( "http://localhost:5000/"
);
  return response.json();
}


getData().then((data) => {
//... your code
})

现场演示

我的代码也有同样的错误,

$.getJson("/foo.json")

问题是我的函数实际上是拼写的

$.getJSON

不是$.getJson

请使用完整版的 jquery CDN 而不是 Slim/lite 版。

 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

没有跨域和完整性,我希望它会起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM