0

I have setup a flask http server properly configured to accept CORS requests (for the purpose of creating a userscript web filter), but am unable to send HTTP requests via tampermonkey, with the following code.

// ==UserScript==
// @name        CORS test
// @namespace   John Galt
// @version     1
// @grant       none
// ==/UserScript==

var request = new XMLHttpRequest();

request.open("GET", "127.0.0.1:5000", false);

request.send();

alert("Finished");

"Finished" is not alerted indicating the failure of the operation, and the following error appears in the console:

XMLHttpRequest cannot load %3127.0.0.1:5000. Cross origin requests are only supported for HTTP. rand.html:1
ERROR: Execution of script 'CORS test' failed! Failed to execute 'send' on 'XMLHttpRequest': Failed to load '%3127.0.0.1:5000'.
Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load '%3127.0.0.1:5000'.
    at Error (native)
    at Object.eval (eval at <anonymous> (unknown source), <anonymous>:19:9)
    at Object.eval (eval at <anonymous> (unknown source), <anonymous>:23:4)
    at e (<anonymous>:41:76)
    at eval (eval at <anonymous> (unknown source), <anonymous>:1:24)
    at eval (native)
    at m (<anonymous>:68:246)
    at Z (<anonymous>:41:92)
    at aa (<anonymous>:78:392)
    at b (<anonymous>:79:109)

I have no experience with front-end so could someone point out if this is a problem with my server configuration or something on the front-end, and if so, is there a workaround?

John Galt
  • 487
  • 1
  • 5
  • 11

2 Answers2

0

The first line is the problem. You can only use XMLHttpRequest on the same domain that the code is on, it can't fetch from other origins.

Here is one alternative solution How fix cross origin error in extjs6?

Community
  • 1
  • 1
Mousey
  • 1,855
  • 19
  • 34
0

I know this is old, but the one answer is actually wrong.

The first sentence in the posted error response contains the truth %3127.0.0.1:5000, where %3 is usually a control character. Cross origin requests are only supported for HTTP.

The URL should be should be http://127.0.0.1:5000, and to a certain degree Chrome now (65, June 2018) requires https://.

Paul Wratt
  • 51
  • 5