javascript - 使用 API 时出错

标签 javascript json api

我是 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/

相关文章:

javascript - Angular JS路由不起作用

javascript - 将项目数组减少为单个数组列表

javascript - 为 JavaScript 值分配变量

javascript - 如何从谷歌翻译中获取所选语言

javascript - 无效的 Json 字符串 Google 可视化图表

json - Golang直接在模板中使用json

php - 将 REST API 中的 CRUD 限制为所有者

javascript - Ext JS数据转换

php - Fortnite Tracker API 仅在本地计算机上运行?

java - REST API Url 中的额外查询参数