[英]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.js
是document.ready
所在的位置。
謝謝!
您似乎使用的是slim
的jquery
版本,它沒有getJSON
方法,這就是您收到此錯誤的原因。
請使用以下鏈接中的完整版jquery
代替。
https://code.jquery.com/jquery-3.1.1.min.js
Slim
版jquery
不包括ajax
、 animations 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 燒瓶中為我工作
您還可以將Fetch
與async
和await
一起使用:
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.