3

Is it possible to call the post-method on a mongodb (mongolab.com server) directly from a chrome extension script?

I need this because I have a separate server-script I don't want users to get access to. I'd like to just post and get from this server directly from the extension.

I did as recommended by @mnemosyn, and still have some problems. My extension won't post to my DB. Code snip below being called from background-script:

var xhr = new XMLHttpRequest();
xhr.open("POST", "MY-API-URL", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send('{"something": "inJsonHere"}');
Styx
  • 9,863
  • 8
  • 43
  • 53
chrisv
  • 724
  • 6
  • 18
  • Uargh, never skip the server! Mongolab is a mongodb hoster, you don't want to expose the database to the public internet at all. Implement that hidden method in your backend code and authorize calls to it using some web standard auth scheme, such as OAuth or HTTP Basic auth. Hitting the DB directly is way to dangerous, and two-tier architectures are almost always a bad idea. – mnemosyn Dec 02 '14 at 13:27

1 Answers1

4

Solved by creating an empty local variable, and then setting the variable to be on a json-format like this:

var json = '';
json = '{"url":' + '"' + URL-FROM-OPEN-TAB + '"' + '}';
var xhr = new XMLHttpRequest();
    xhr.open("POST", "MY-API-URL", true);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(jsonUrl);

Don't know why this worked and not the other, but I'm happy!

chrisv
  • 724
  • 6
  • 18