我正在使用 Flaskr 通过 RESTful API 生成数据。我的电话看起来像:
http get localhost:5000/v1.0/dataset dataset_id==f7e7510b3c1c4337be339446ca000d22
并返回类似:
{"sites": "a"}
现在我正在尝试使用我的网络应用程序获取这些数据。我首先遇到了一个跨域错误,但经过一番阅读,发现我可以通过使用 jsonp 绕过该错误。基本上复制我在这里找到的一段代码,我把它放在一起(我是 JavaScript 新手):
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
(function($) {
var url = 'http://localhost:5000/v1.0/dataset?dataset_id=f7e7510b3c1c4337be339446ca000d22&callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.dir(json.sites);
},
error: function(e) {
console.log(e.message);
$('#data').html('the error was thrown');
}
});
})(jQuery);
</script>
</head>
<body>
<div id = 'data'></div>
<p> place holder </p>
</body>
并相应地将我的 python 响应更改为:
"jsonCallback({\"sites\":\"a\"});"
如果这有帮助,我的烧瓶返回线如下:
return callback_function + '({"sites":"a"});'
我相当有信心我的 python 方面的问题是好的,但我对 JS 的了解不够,无法确定错误来自哪里。我的目标是简单地在页面上显示我的数据。