如何在 JavaScript 中将 Ajax 转换为 Fetch API?

wob*_*ano 6 javascript ajax jquery fetch-api rivescript

所以我使用RiveScriptJavaScript 端口,它使用 ajax,当然我不想再使用 jQuery。只有一行 ajax,我想将其更改为新的 Fetch API。

**FYI: You can see the ajax code in line 1795 of the CDN.**
Run Code Online (Sandbox Code Playgroud)

所以这是原始代码:

return $.ajax({
    url: file,
    dataType: "text",
    success: (function(_this) {
        return function(data, textStatus, xhr) {
            _this.say("Loading file " + file + " complete.");
            _this.parse(file, data, onError);
            delete _this._pending[loadCount][file];
            if (Object.keys(_this._pending[loadCount]).length === 0) {
                if (typeof onSuccess === "function") {
                    return onSuccess.call(void 0, loadCount);
                }
            }
        };
    })(this),
    error: (function(_this) {
        return function(xhr, textStatus, errorThrown) {
            _this.say("Ajax error! " + textStatus + "; " + errorThrown);
            if (typeof onError === "function") {
                return onError.call(void 0, textStatus, loadCount);
            }
        };
    })(this)
});
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止使用 Fetch API 尝试过的:

return fetch(file, {
        dataType: "text"
    })
    .then(function(_this) {
        return function(data, textStatus, xhr) {
            _this.say("Loading file " + file + " complete.");
            _this.parse(file, data, onError);
            delete _this._pending[loadCount][file];
            if (Object.keys(_this._pending[loadCount]).length === 0) {
                if (typeof onSuccess === "function") {
                    return onSuccess.call(void 0, loadCount);
                }
            }
        };
    })
    .catch(function(_this) {
        return function(xhr, textStatus, errorThrown) {
            _this.say("Ajax error! " + textStatus + "; " + errorThrown);
            if (typeof onError === "function") {
                return onError.call(void 0, textStatus, loadCount);
            }
        };
    })
Run Code Online (Sandbox Code Playgroud)

应用程序代码:

var bot = new RiveScript();

bot.loadFile("./brain.rive", loading_done, loading_error);


function loading_done (batch_num) {
    console.log("Batch #" + batch_num + " has finished loading!");

    bot.sortReplies();

    var reply = bot.reply("local-user", "Hello, bot!");
    console.log("The bot says: " + reply);
}

function loading_error (error) {
    console.log("Error when loading files: " + error);
}
Run Code Online (Sandbox Code Playgroud)

使用 Fetch API,我现在没有看到任何错误,尽管我也没有看到任何错误或成功消息。

我在这里错过了什么吗?

sid*_*ker 6

获取初始化对象不具有dataType关键。

要指示您想要纯文本,请Accept: text/plain向请求添加标头:

fetch(file, {
    headers: {
      "Accept": "text/plain"
    },
  })
Run Code Online (Sandbox Code Playgroud)

fetch调用返回一个承诺,以做出决议Response的对象,而Response对象提供的方法与决心文本JSON数据,或者Blob-这意味着基本形式用于处理来自您的响应fetch(…)调用是这样的:

fetch(file, {
  headers: {
    "Accept": "text/plain"
  },
})
.then(response => response.text())
.then(text => {
  // Do something with the text
})
Run Code Online (Sandbox Code Playgroud)

因此,您需要采用问题中的现有代码并将其放入该表单中。