简体   繁体   中英

Trouble Converting cURL request to Javascript

I don't know what's wrong. I've spent almost an hour reading and re-reading, checking my spelling etc. I was hoping maybe someone can point out what I'm doing wrong.

This is the cURL statement that returns successfully in the terminal:

curl https://api.gumroad.com/v2/products \
  -d "access_token=123456abcdef" \
  -X GET

The following are some of my attempts that did not work. And yes, I'm certain jQuery has been loaded:

$.ajax({
    url: "https://api.gumroad.com/v2/products",
    data: "access_token=123456abcdef",
    success: function(result){
        console.log(result);
    }});

And this one:

$.ajax({
    url: "https://api.gumroad.com/v2/products",
    data: "access_token=123456abcdef",
    processData: false,
    type: "get",
    success: function(result){
        console.log(result);
    }});

And another one:

$.ajax({
        url: url,
        beforeSend: function(xhr) {
             xhr.setRequestHeader("access_token", "123456abcdef")
        }, success: function(data){
            alert(data);
            //process the JSON data etc
        }
})

Looks like you are sending a string instead of a data object, try this:

$.ajax({
  url: "https://api.gumroad.com/v2/products",
  data: {access_token: "123456abcdef"},
  processData: false,
  type: "get",
  success: function(result) {
    console.log(result);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM