2017-08-31 93 views
0

根據以下代碼,我必須得到一個虛擬的JSON響應,但它卻向我顯示一個錯誤。爲什麼?沒有得到預期的虛假JSON響應,但錯誤

<html> 
 
<head></head> 
 
<body> 
 
<script> 
 
var root = 'https://jsonplaceholder.typicode.com'; 
 

 
$.ajax({ 
 
    url: root + '/posts/1', 
 
    method: 'GET' 
 
}).then(function(data) { 
 
    console.log(data); 
 
}); 
 
</script> 
 
</body> 
 
</html>

參考: https://jsonplaceholder.typicode.com/

在此先感謝。

+1

根據片段中,你缺少jquery。你必須在使用它之前加載它。 – insider

+1

您正在使用Jquery。請在您的html中添加jquery.js腳本文件引用! –

回答

1

的錯誤是:

{ "message": "Uncaught ReferenceError: $ is not defined",
"filename": " https://stacksnippets.net/js ", "lineno": 17, "colno": 1 }

所以你需要添加的jQuery的腳本是這樣的:

<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
</head> 

您還可以看看jQuery的page

+0

輸入「look」而不是「loot」 – Deadpool

+0

@Deadpool謝謝。 –