Struggling with API's

Hello everyone ! As a lot of people, i’ve been struggling with API’s for almost a day know, looking for tutorials that end up being outdated, trying to figure out a code by mocking other people works up, before finally giving up and crashing up here, hoping that someone will come at our rescue.

I’ve tried my best to nailing it on my own, and wrote this function :

function getquote(){
  $.ajax({
    type: "GET",
    url: "https://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=json&lang=en",
    success : function(quote){
    quote = JSON.parse(quote); 

    CQuote = quote.message;
    CAuthor = quote.quoteAuthor;
      document.getElementById("test").innerHTML = CQuote + CAuthor;
    },
    error : document.getElementById("test").innerHTML = "error"
  });
}

but nothing I try doesn’t end up with an “error” string laughing at my face.

Could someone a little bit experienced could help me figure out what i’m doing wrong ? That would be much helpfull, thank you !

I remember doing this project and having trouble with the Forismatic API. The API is a little different and using JSON doesn’t seem to work so you have to use JSONP.
Here is the code I used to achieve the ajax request:

$.ajax({
      jsonp: "jsonp",
      dataType: "jsonp",
      url: "https://api.forismatic.com/api/1.0/",
      contentType: "application/jsonp",
      data: {
        lang: "en",
        method: "getQuote",
        format: "jsonp"
      },
      success: function(data) {

}

or an edited version of your code:

function getquote() {
      $.ajax({
        jsonp: "jsonp",
        dataType: "jsonp",
        contentType: "application/jsonp",
        url: "https://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&lang=en",
        success : function(quote){

        CQuote = quote.message;
        CAuthor = quote.quoteAuthor;
          document.getElementById("test").innerHTML = CQuote + CAuthor;
        },
        error : document.getElementById("test").innerHTML = "error"
      });
    }

Hey, thanks that worked great ! I’m still totally lost tough, since I tried with several other API with no success. I guess i’ll try to use jsonp more often in the future.

Also, could I ask you a question ? When visiting a the api via the browser nav bar, i always typed in the data separated from the URL with a “?” but if I try to access the jsonp version of the api I have to add “&jsonp=?” and I guess this is no data. Do you have any idea ?

I believe you wouldn’t have to add that to the URL of the webpage considering it has been done in the code. Also, you have already specified what data type you want it to send to you. You don’t have to worry so much as this API is just confusing and tons of people have had problems with it. In this case, just use JSONP but in most other cases you will be using JSON or XML.

1 Like