简体   繁体   English

我可以针对此API使用jquery的ajax方法而非unirest吗?

[英]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. 这是我要使用的API的说明。 It recommends unirest but I can't install node on my server. 它建议使用unirest,但我无法在服务器上安装节点。

https://www.wordsapi.com/docs/?javascript# 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. 但我收到该请求的401未经授权的结果。 The X-Mashape-Key header is included in the call with my api key, but it doesn't seem to want to accept it. X-Mashape-Key标头包含在使用我的api键的调用中,但似乎不想接受它。 Does unirest do something that this jquery ajax call doesn't? unirest会执行此jquery ajax调用不执行的操作吗?

Try with 试试看

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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