5

I am getting this error as part of this larger puzzle here.

var xhr = new XMLHttpRequest();
xhr.setRequestHeader( 'Content-Type', 'application/json' );

//Error: INVALID_STATE_ERR: DOM Exception 11

For further research

  1. O'Reilly's book "Definite Guide to Javascript 6th Edition" on page 491 in chapter 18 "Scripted HTTP" discussed XMLHttpRequest, please, note that it is not only about HTTP or XML (historical relics).

  2. Mozilla's dev entry about XMLHttpREquest here

Community
  • 1
  • 1
hhh
  • 50,788
  • 62
  • 179
  • 282
  • possible duplicate of [INVALID_STATE_ERR: DOM Exception 11 (WebKit)](http://stackoverflow.com/questions/3488698/invalid-state-err-dom-exception-11-webkit) – Šime Vidas Jun 18 '12 at 13:15
  • Have you tried searching Stack Overflow for "DOM 11"? There already exist a bunch of threads addressing this issue. – Šime Vidas Jun 18 '12 at 13:16
  • 3
    @ŠimeVidas: That's not a good duplicate. INVALID_STATE_ERR/11 is used for *so many things*... – Ry- Jun 18 '12 at 13:17

1 Answers1

6

You need to open() the XMLHttpRequest before you can set request headers. Just move that line to after you call open():

var xhr = new XMLHttpRequest();
xhr.open( 'POST', 'example.php', true );
xhr.setRequestHeader( 'Content-Type', 'application/json' );
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Suppose I just run my code in Browser console, what should the `"example.php"` be in that case? The Mozilla Dev -site has some monster `"url += ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime()"` but probably not something here? (sorry this may be a dump question, still learning/reading stuff...) – hhh Jun 18 '12 at 13:36
  • 1
    @hhh: Whatever page it is you want to request - but it has to be in the same domain name, or you'll get an exception. – Ry- Jun 18 '12 at 13:38
  • There is one problem, there is no page. My purpose in the other question is to add content to the db in the address `http://127.0.0.1:5984/test`, couchDB can be accessed like that. – hhh Jun 18 '12 at 13:41
  • @hhh: If there's no page, where is your JavaScript running? Just any old website that you happen to have a Developer Tools window open on? – Ry- Jun 18 '12 at 13:43
  • Yes, on any page really `CTRL+SHIFT J` and copy-pasting the code on any page, even the loading Chromium -page having only the windows about historical browsing -- its source is `view-source:chrome://newtab/`. Any placeholder for some generic solution? – hhh Jun 18 '12 at 13:49
  • 1
    @hhh: If CouchDB is set up for easy JavaScript access (I don't know), just try putting in that URL. Otherwise, you'll need to send some extra headers - read up on CORS. Or, if this is just for yourself, you can start up instances of Chrome[ium] with the flag `--disable-web-security` - but don't browse anywhere else with that. – Ry- Jun 18 '12 at 13:51