简体   繁体   English

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

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

I've got this piece of code:我有这段代码:

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

localhost:5000 is a flask/python script that returns a json like: localhost:5000 是一个flask/python脚本,它返回一个json ,如:

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

And I'm getting:我得到:

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

Any tips where I can start untangling the whoolball?有什么技巧可以让我开始解开排球吗?

Thanks!谢谢!

Edit:编辑:

HTML: 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.js is where the document.ready is located. lib/main.jsdocument.ready所在的位置。

Thanks!谢谢!

You seem to be using slim version of jquery which does not have the method getJSON thats why you are getting this error.您似乎使用的是slimjquery版本,它没有getJSON方法,这就是您收到此错误的原因。

Please use the full version of jquery instead from the below link.请使用以下链接中的完整版jquery代替。

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

Slim version of jquery excludes ajax , animations effects etc Slimjquery不包括ajaxanimations effects

Thus happens commonly when migrating codebases from jQuery v1.x+ to v3.0+ as jQuery updated/deprecated/discontinued some of it's API.因此,在将代码库从 jQuery v1.x+ 迁移到 v3.0+ 时经常发生这种情况,因为 jQuery 更新/弃用/停止了它的一些 API。

I recommend using jQuery Migrate which will address this, along with other issues:我建议使用 jQuery Migrate 来解决这个问题以及其他问题:

Get it here via CDN:通过 CDN 在此处获取:

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

If using Gulp/Grunt, you can import into your project using如果使用 Gulp/Grunt,您可以使用导入到您的项目中

npm install --save jquery jquery-migrate

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

Read more about jQuery v3.0+.. http://blog.jquery.com/2016/06/09/jquery-3-0-final-released/阅读更多关于 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');  
});

This work for me in python flask在 python 烧瓶中为我工作

You can also use Fetch with async and await :您还可以将Fetchasyncawait一起使用:

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


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

Live demo现场演示

I had the same error with my piece of code,我的代码也有同样的错误,

$.getJson("/foo.json")

The issue was that my function is actually spelled问题是我的函数实际上是拼写的

$.getJSON

NOT $.getJson .不是$.getJson

Please use the full version of jquery CDN instead Slim/lite version.请使用完整版的 jquery CDN 而不是 Slim/lite 版。

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

without cross-origin and integrity, I hope it will work没有跨域和完整性,我希望它会起作用

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

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