I
I
Ilya Kanatov2017-05-20 12:21:52
JavaScript
Ilya Kanatov, 2017-05-20 12:21:52

How to connect to Gumroad API using AJAX?

Friends, I'm completely desperate: I can't figure out how to write a JS script correctly to get data from the Gumroad API.
The API docs say they respond with JSON, however I can't use a JSON request as this results in an error

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).

although I have substituted this header in the header in previous attempts .
As far as I understand, in my case, when there is a request from an unpredictable site, it will not work to use JSON, but they respond using JSON.
So I use JSON-P , then I get a response but I can't access it in code.
Firefox throws an error:
“SyntaxError: missing ; before statement",

and chrome:
"Refused to execute script from 'my request' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled."

I've searched the entire Stackexchange and couldn't find a working tip.
There is also my question
I do not know what to do, please help.
What are the options?
Thank you!
Code in JS
(the token in the example is working)
Version jQuery 1.12.4
Вариант 1

  jQuery.ajax({
    dataType: 'jsonp',
    url: 'https://api.gumroad.com/v2/products',
    data: {
        'access_token': '676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5'
    },

    success: function(data){
        console.log(data);
    },
    error: function(d){
        console.log(d);
    }
  });


Вариант 2

jQuery(document).ready(function() { 
    var url = 'https://api.gumroad.com/v2/products/';
    url += '?method=getQuote';
    url += '&format=jsonp';
    url += '&lang=en&';
    url += 'jsonp=myJsonMethod';
    url += '&?callback=?';
    url += '&access_token=676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5';
    jQuery.getJSON(url);
});

function myJsonMethod(response){
  console.log (response);
}


Вариант 3

function jsonp(url) {
   var head = document.head;
   var script = document.createElement("script");

   script.setAttribute("src", url);
   head.appendChild(script);
   head.removeChild(script);
 }

 jsonp("https://api.gumroad.com/v2/products?callback=jsonpCallback&access_token=676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5");

function jsonpCallback(data) {
  document.getElementById("response").textContent = JSON.stringify(data);
}

Request
https://api.gumroad.com/v2/products?callback=jQuery112409655243732650752_1495261525390&access_token=676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5&_=1495261525391

Titles
RVFvG.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Kanatov, 2017-05-21
@dukenuk

Decided on SE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question