【发布时间】:2018-03-14 02:50:43
【问题描述】:
我正在尝试使用来自 fiddle 的 JQuery ajax 调用从 this page 加载资源
我的代码在这里:
$('button').on('click', function() {
$('#message').text('');
$.ajax({
url: "http://pool.burstcoin.ro/pending2.json",
type: "GET",
crossDomain: true,
dataType: "json",
success: function(response) {
console.log(response);
$('#message').text(response.message);
},
error: function(xhr, status) {
alert("error");
}
});
});
我创建了fiddle 只是为了看看我是否可以得到任何类型的响应,但我得到了这个 js 错误响应: 请求的资源上没有“Access-Control-Allow-Origin”标头。 我已经阅读了一些关于此的帖子,如果我没记错的话,不可能在前端做任何事情来添加缺失的标题。是否可以在 Node.js(或其他一些 JavaScript 框架)中将正确的标头添加到响应中? 如果是这样,我真的很感激一个更新的小提琴,因为我自己缺乏知识来证明这一点。
【问题讨论】:
-
服务pool.burstcoin.ro/pending2.json的服务器需要添加Access-Control-Allow-Origin响应头。
-
只需将您的数据类型更改为“jsonp”:jsfiddle.net/o5yq4ajn/4
标签: javascript jquery node.js ajax