我是 API 的新手,他们似乎总是挂断我的电话。我不断收到以下错误:
“Access-Control-Allow-Origin” header 的值为“http://null” ' 不等于提供的原点。因此不允许访问 Origin 'null'。
这是我的 JS:
var url = "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1";
$('#quotebutton').on('click', function(e) {
e.preventDefault();
$.ajax( {
url: "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1",
success: function(data) {
var post = data.shift(); // The data is an array of posts. Grab the first one.
$('#quote-title').text(post.title);
$('#quote').html(post.content);
// If the Source is available, use it. Otherwise hide it.
if (typeof post.custom_meta !== 'undefined' && typeof post.custom_meta.Source !== 'undefined') {
$('#quote-source').html('Source:' + post.custom_meta.Source);
} else {
$('#quote-source').text('');
}
},
cache: false
});
});
最佳答案
如@moataz-elmasry 所述,js 功能很好,但 API 有访问限制。 运行相同代码的 fiddle 运行得很好: https://jsfiddle.net/9gtbsp2e/1/
*我更新了链接以使用 https
来消除安全错误
关于javascript - 使用 API 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46838600/