The problem is that whatever I use for the filename (I’ve tried
logins.json
/logins.json
file://logins.json
file:///logins.json
the “fail” lambda gets called with the empty object {} and status “Error” and no other information. Im obviously calling “get” wrong, but don’t know how to fix it.
(BTW: I don’t know if my markup is working – I didn’t see a “preview” button.)
I’m not very familiar with $.get, but looking at the jquery docs, it seems like the callback function only takes one argument (data), so I’m not sure what your status variable is doing. I’m not sure if that’s what’s throwing the error.
Theres also this bit regarding the fail method:
If a request with jQuery.get() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method. Alternatively, as of jQuery 1.5, the .error() method of the jqXHR object returned by jQuery.get() is also available for error handling.
Edit:
After researching a bit more, that info is a bit outdated since I guess error() is deprecated by fail(). But the information on fail() doesn’t show that it takes any arguments.
It looks like using ajaxError() really is the correct way to get an error message. Here’s an example taken from the docs page for it.
First, the file:// scheme won’t work at all. You have to use an actual web server and open the game from the server, even if it’s on local machine. Browsers have the security measures that say sites are not allowed to read anything local.
Second, try logins.json or ./logins.json
Also if the server treats json as MIME type application/json jQuery can automatically parse the file when it reads it so no JSON.parse needed. Of course, that depends on jQuery version.
OK. It works if I use “logins.json” and use http: as the URL of the twine generated HTML from a web server, but doesn’t work if I use “logins.json” with a file: URL to run the Twine. This means I have to publish the story to a file and then copy it to the web server area, rather than test it from within the Twine tool. I can work with that.
The JSON parsing is still not working but probably just because what @oreolek says about JSON parsing being automatic.
Rather than posting a bunch of code here, take a look at the JavaScript section in my Local Storage Manager code (import the HTML into Twine), which is intended to let you export/import a game’s entire SugarCube save slot list. It uses the FileSaver.js code as well.
Just let me know if you have any questions on it.
[EDIT]
P.S. Oh, I forgot that this isn’t mentioned in that code. The output file is written like that so that you can use the importScripts() function to load it, instead of using a dialog box, if you know the path and filename. If you’re unfamiliar with how to use that function, see my “Loading External Scripts” sample code (click “Jump to Start” in the UI bar to see other sample code there).