Chrome扩展调用Anki API添加卡片时遭遇403拒绝的问题求助
Chrome扩展调用Anki API添加卡片时遭遇403拒绝的问题求助
大家好,我正在开发一款Chrome扩展,目标是创建Anki卡片并将其添加到我的卡组中。目前我卡在了调用Anki API的环节——服务器一直拒绝我的请求,返回403 Forbidden错误,折腾了好久都没解决,来这儿求助各位大佬!
我用JavaScript写了一段硬编码卡片内容的代码,尝试向Anki的本地API地址http://localhost:8765发送请求,代码如下:
async function createCard() { // Set the Anki API endpoint URL const baseURL = 'http://localhost:8765'; // Set the Anki API action, version and params const card = { "action": "addNote", "version": 6, "params": { "note": { "deckName": "Default", "modelName": "Basic", "fields": { "Front": "front content", "Back": "back content" }, "options": { "allowDuplicate": false, "duplicateScope": "deck", "duplicateScopeOptions": { "deckName": "Default", "checkChildren": false, "checkAllModels": false } } } } }; // Send the request to the Anki API try { const response = await fetch(baseURL, { method: 'POST', mode: 'no-cors', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(card) }); // Check the status code of the response if (response.ok) { console.log('Card created successfully'); } else { console.error(`Error creating card: ${response.statusText}`); } } catch (error) { console.error(`Error creating card: ${error}`); } }
执行这段代码时,Chrome控制台抛出了两个错误:
- 第一个错误出现在fetch函数调用处:
POST http://localhost:8765/ net::ERR_ABORTED 403 (Forbidden) - 第二个错误来自
console.error(Error creating card: ${response.statusText});这一行,输出内容是Error creating card:,我推测这个错误是第一个403错误导致的,毕竟请求根本没成功完成。
作为编程新手,我目前尝试过的解决方法有:
- 查看Anki的日志文件,试图找到错误相关的信息,但没找到任何有用内容
- 调整卡片参数的语法格式,因为报错发生在发送请求的环节,但调整后问题依旧
- 确认Anki的本地服务器处于运行状态,localhost是可以正常访问的
实在没辙了,希望各位能帮我看看问题出在哪儿,谢谢大家!
备注:内容来源于stack exchange,提问作者programmerare
相关产品推荐
相关产品推荐

