Having moved to a new laptop, the connection I had between my notes and Anki stopped working.

Obsidian, the note taking app I use, has a plugin that allows me to generate Anki flashcards directly from my notes. It uses Anki Connect, an Anki plugin that exposes an API at localhost:8765. That worked fine on my old laptop, when I first connected Obsidian to Anki. On my new laptop it did not work, with the Obsidian plugin saying it could not connect to Anki. Anki Connect is active though.

Obsidian has a developer view (In the menu View go to Toggle developer tools), with a tab Console that shows what’s going on under the hood.
There I found the error message Access to XMLHttpRequest at 'http://127.0.0.1:8765/' from origin 'app://obsidian.md' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost' that is not equal to the supplied origin., which meant nothing to me but allowed me to search online for more background.

I run MAMP Pro, which provides me with a local web and mysql server on my laptop. The error to me implied MAMP was refusing the connection based on the source of the request: an app.

The suggested solution I found was changing the server settings to be less strict, in the Directory Directive of MAMP’s Apache webserver settings. I can do that directly in the Apache httpd.conf file in /MAMP/conf/apache, and I can do that in the MAMP interface, selecting localhost and the Apache tab, which has a field “Additional parameters for the Directory Directive”.
Neither solves the issue however, and the error message remains the same. Then I shut down MAMP entirely, to see what happens then. The same error occurs. So, the request was not being handled by MAMP at all!

MacOS 11.0 Big Sur comes with Apache 2.4 pre-installed, so I looked there (in /etc/apache2)her. Anki Connect doesn’t use that Apache instance either.

Then I went back to the Anki Connect plugin itself. Getting to the files of that plugin is best done through Anki itself, selecting the Tools menu, and Add-ons. Then in the screen select Anki Connect and the View Files button. Anki’s plugin files are stored in folders not named after the plugin but after the plugin identification number, so you won’t find anything by searching your file system with the plugin name. There’s a file there config.json, which can also be accessed directly in the Anki add-ons screen through the Config button. And there at the bottom it has the ‘webCorsOriginList‘ that only has localhost which corresponds with the console error message at the start. I had looked at this config file already before going on a wild goose chase in MAMP and Apache settings. Seeing it again having exhausted the previous paths, it hit me in the face: I need to expand that list. And at the same moment I realised I had done that on my old laptop too, because it clearly said so in the installation guide. Duh! Having done what the manual says, it works again.

The issue had nothing to do with local webservers, Anki and Obsidian can do it on their own, if only I had followed the documented settings.
TIL, and definitely not for the first time: Stop and think first. Read the manual. Would have saved me significant time today.

Blogged (and noted) as a reminder.

2 reactions on “CORS Error Connecting to Anki, Or How to Stop and Think First

  1. @ton Dat zou voor mij één van de redenen zijn om het niet op een laptop lokaal te draaien – niet alleen is instellen ineens een dingetje als je die laptop om welke reden dan ook herinstalleert, het is ook een dingetje als die laptop onverhoopt eens gejat wordt.Niet toevallig nog ergens een server-achtig apparaat slingeren waar zulks kan worden ondergebracht?

  2. @doenietzomoeilijk nu ja uiteindelijk zat ik gewoon op het verkeerde spoor, en kan Anki gewoon met Obsidian praten als ik de gedocumenteerde instellingen voor beiden gevolgd had. Ik dacht alleen maar dat ik het in de lokale webserver moest zoeken. Dus in die zin is er niets aan de hand, zowel Obsidian als mijn Anki deck zijn ook nog beschikbaar als mijn laptop wegvalt/gejat wordt. Verder draai ik niets kritisch op die lokale webserver

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.