I’m trying to use Gumroad API for WordPress plugin, and trying to understand how to make any call, GET in my case.

I searched a lot for code examples on stack overflow, but still can’t run it without an errors.

Plain JS or jQuery.ajax is acceptable.

Code:

    function createCORSRequest(method, url) {
      var xhr = new XMLHttpRequest();

      if ("withCredentials" in xhr) {
        // XHR for Chrome/Firefox/Opera/Safari.
        xhr.open(method, url, true);
      } else if (
        typeof XDomainRequest != "undefined") {
        // XDomainRequest for IE.
        xhr = new XDomainRequest();
        xhr.open(method, url);
      } else {
        // CORS not supported.
        xhr = null;
      }
      return xhr;
    }

    var xhr = createCORSRequest('GET', 'https://api.gumroad.com/v2/products');
    xhr.onreadystatechange=function(){
      if(xhr.readyState==4 && xhr.status==200){
        consolelog(xhr.responseText);
      }
    }

    xhr.open("GET","https://api.gumroad.com/v2/products",true);
    xhr.setRequestHeader('Authorization', 'Basic');
    xhr.setRequestHeader('Accept-Language', 'en_US');
    xhr.setRequestHeader("Content-type","application/json");
    xhr.setRequestHeader("Access-Control-Allow-Origin","*");
    xhr.setRequestHeader("Access-Control-Allow-Methods","GET");

    xhr.send('676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5'); // The token is real

The error is:

Cross-Origin Request Blocked:
The Same Origin Policy disallows reading the remote resource
at https://api.gumroad.com/v2/products.
(Reason: CORS header ‘Access-Control-Allow-Origin' missing).

Response:

error {
  target: XMLHttpRequest,
  isTrusted: true,
  lengthComputable: false,
  loaded: 0,
  total: 0,
  currentTarget: XMLHttpRequest,
  eventPhase: 2,
  bubbles: false,
  cancelable: false,
  defaultPrevented: false,
  composed: false
}

Thanks a lot for any suggestions!

Read more here: Cross-Origin Request Blocked in oauth api request


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: