thesaurus & dictionary > Support > How to set request headers for jquery ajax call

Search

Topic closed
User Post
harith wannigama
3 post(s)


Hi, 

I'm a student from New Zealand. First off, I wanna say thanks for the API! I'm currently trying to use your in my project (a web app), but I'm having trouble determining how I should call your API through ajax. The intention is to pass a 'word' (string) into a function and this word is then processed by your API and sends back a list of synonyms of that word.

 I tried using PostMan to check how your API works, and I came across something: When I simply include the http type as "GET" and the request url to: "http://thesaurus.altervista.org/thesaurus/v1?word=happy&language=en_US&output=json&key=MY_KEY" it seems to work and return the correct output. However, I am unsure and unable to to simply pass the url as "http://thesaurus.altervista.org/thesaurus/v1" and then have request headers that satisfy required parameters i.e 'key' : 'MY_KEY', 'word': 'happy', 'language': 'en_US'

This is what my code looks like:

<font face="verdana, arial, helvetica, sans-serif">
function findSimilarWords(word, callback) {
    $.ajax({  
        url: "http://thesaurus.altervista.org/thesaurus/v1",
        type: "GET",
        
        beforeSend: function (xhrObj) {
            // Request headers
            xhrObj.setRequestHeader("key", "MY_KEY"); // where MY_KEY is the key I got from your website 
            xhrObj.setRequestHeader("word", word);
            xhrObj.setRequestHeader("language", "en_US");
            xhrObj.setRequestHeader("output", "json");
        }, 
        dataType: 'json',
    })
        .done(function (data) {
            if (data.length != 0) {
                var unsplitSynonyms: string;
                var synonyms: string[] = [];
                unsplitSynonyms = data.response[0].list.synonyms; // take first set 
                synonyms = unsplitSynonyms.split(" (similar term)|", 2);
                callback(synonyms);

            /*
            for (key in data.response) { 
                list = data.response[key].list; 
                synonyms += list.synonyms; 
             }
             */
            } else {
                pageheader.innerHTML = "The process did not seem to work -- no synonyms generated!";
            }
        })
        .fail(function (error) {
            pageheader.innerHTML = "It appears something has gone wrong...";
            console.log(error.getAllResponseHeaders());
        });
}
</font>


Sorry if this is not the right place to ask (or if it's even okay to ask). Any help would be greatly appreciated



03-Sep-16, 13:48:41 Rispondi | Citazione | Report spam
admin
145 post(s)


Web site
I've made a minor change in the server to support jQuery ajax requests, please retry.

In any case you can see a working example (and download it) here: http://thesaurus.altervista.org/ajax.html


03-Sep-16, 16:27:57 Rispondi | Citazione | Report spam
harith wannigama
3 post(s)


Thank you! I will have a try and get back to you on how it goes. 


04-Sep-16, 6:08:53 Rispondi | Citazione | Report spam
harith wannigama
3 post(s)


Fantastic, it works like a charm now! Thanks again! 


04-Sep-16, 6:19:17 Rispondi | Citazione | Report spam