javascript - Uploading documents to CouchDB in pure JS -
i'm writing game in web browser, , i'd make highscores list. i'd save highscores users can compete, i've created database in cloudant (couchdb) plan upload & download scores.
function uploadhighscores() { var req = new xmlhttprequest(); req.open('put', 'http://reverse-snake.cloudant.com/highscores/highscores', true); // method, url, async req.onload = function() { // asynchronous callback. console.log(req.responsetext); console.log("scores uploaded"); } req.setrequestheader('content-type', 'application/json'); console.log("sending scores..."); req.send("{\"_id\": \"highscores\", \"_rev\": \"6-f29fd233a21c65e3f4a7485f36c46ae4\", \"data\":{\"scores\":[999, 999, 999, 999, 999], \"times\":[[59, 59, 999], [59, 59, 999], [59, 59, 999], [59, 59, 999], [59, 59, 999]]}}"); } this fails error:
xmlhttprequest cannot load http://reverse-snake.cloudant.com/highscores/highscores. response preflight invalid (redirect)
i've tried doing request curl:
curl http://reverse-snake.cloudant.com/highscores/highscores -x put -d "{\"_id\": \"highscores\", \"_rev\": \"6-f29fd233a21c65e3f4a7485f36c46ae4\", \"data\":{\"scores\":[999, 999, 999, 999, 999], \"times\":[[59, 59, 999], [59, 59, 999], [59, 59, 999], [59, 59, 999], [59, 59, 999]]}}" and succeeds. missing?
edit: per request, log of failed http request: http://pastebin.com/d97kwpm1
the answer painfully simple. couchdb (and cloudant) require secure network access. revealed network logs:
get requests went http://... , replied redirect; lead https://...
put requests went http://... , replied redirect; lead failure.
the change needs made on line 2, changing method. setting request headers not required.
Comments
Post a Comment