1

I am new to APIs and they seem to keep hanging me up. I keep getting the following error:

The 'Access-Control-Allow-Origin' header has a value 'http://null' that is not equal to the supplied origin. Origin 'null' is therefore not allowed access.

Here is my 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
    });
  });
1
  • I just tried your url in a restles client and I get the error : "Sorry, you are not allowed to post on this site.". Looks like the POST request of quotesondesign.com/wp-json/posts is actually trying to create a post. Please consult their API Commented Oct 19, 2017 at 20:54

1 Answer 1

1

As mentioned by @moataz-elmasry the js function is fine, but the API have access restrictions. Running a fiddle of the same code runs just fine: https://jsfiddle.net/9gtbsp2e/1/

*I updated the link to use https to remove security errors

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.