2017-08-31 104 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谢谢。 –