简体   繁体   中英

Can I use jquery's ajax method instead of unirest for this API?

Here are the instructions for the API I'm trying to use. It recommends unirest but I can't install node on my server.

https://www.wordsapi.com/docs/?javascript#

this is the code I'm trying to use:

$(document).ready(function() {
    $.ajax({
        url: "https://wordsapiv1.p.mashape.com/words/soliloquy",
        type: "GET",
        datatype: "json",
        crossDomain: "true",
        contentType: "application/json; charset=utf-8",
        headers: {"X-Mashape-Key": "XXXXXXX"},
        success: function(result) {
            alert("SUCCESS!");
        },
        error: function(result) {
            alert("FAIL!");
        }
    });
});

but I'm getting a 401 unauthorized result for that request. The X-Mashape-Key header is included in the call with my api key, but it doesn't seem to want to accept it. Does unirest do something that this jquery ajax call doesn't?

Try with

beforeSend: function (xhr) {
   xhr.setRequestHeader ("Authorization", "Basic " + btoa(Your X-Mashape-Key));
},

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