tampermonkeyscalatragm-xmlhttprequest

Can't send data with GM_xmlhttpRequest from Tampermonkey with POST method to Scalatra


... it answers: 500 Server error; java.lang.NoSuchMethodError: 'void org.scalatra.servlet.RichRequest.update(java.lang.Object, java.lang.Object)' at... at... at... ...
My code:

      const ajaxobj =
          { method: "POST"
          , url: url
          , data: '{"q":"anyad"}'   //JSON.stringify(adat)
          , dataType: "json"
          , headers: { "Content-Type": "application/json" }
          , onload: (resp) => { console.log(resp); ... }
          }
      console.log(ajaxobj)
      GM_xmlhttpRequest(ajaxobj)

And in the answer of console.log(ajaxobj) I see:

data: Object { value: "{\"q\":\"anyad\"}" }

instead of a string, an object whose "value" member is the string. I think this is what scalatra doesn't like, because I can send it a string from elsewhere (Angular) and it works like a charm.
And now the interesting thing:
If I don't call the GM_xmlhttpRequest call (comment it out), then data will be string, not object.


Solution

  • I found a practical solution (but how it works, why it's good, I can't understand the theory behind): Fake a pure string (non-JSON) transmit; remove the properties dataType: "json" and headers: { "Content-Type": "application/json" } from the ajax object.