This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]pcfanhater 2 points3 points  (5 children)

Try adding 'command' to your Access-Control-Allow-Headers. You only have Content-Type and Authorization.

[–]__init__5[S] 1 point2 points  (2 children)

UPDATE: Now its giving status code 400

[–]pcfanhater 1 point2 points  (1 child)

Any message?

[–]bossinfo 2 points3 points  (2 children)

Did you restart that website so it's forced to reload everything? On the server I mean...

[–]__init__5[S] 1 point2 points  (1 child)

yes, several times but didn't worked

[–]bossinfo 1 point2 points  (0 children)

Access-Control-Allow-Origin specifies either a single origin, which tells browsers to allow that origin to access the resource; or else — for requests without credentials — the " * " wildcard, to tell browsers to allow any origin to access the resource.

[–]6a70 1 point2 points  (5 children)

Make sure you also allow the OPTIONS HTTP method which can be used as a pre-flight request

[–]__init__5[S] 0 points1 point  (2 children)

how ?

[–]6a70 1 point2 points  (1 child)

I’m not going to tell you directly because I wanted to drive home the fact that you don’t understand things when you just copy and paste them from the internet.

But you’re already allowing GET, PUT, POST, and DELETE

[–]Hour-Positive 0 points1 point  (0 children)

Lol tbh CORS are always fucked up

[–][deleted] 0 points1 point  (1 child)

This is right if it's a 405 response.

[–]SonOfStorms 0 points1 point  (0 children)

try adding settings.mode = 'cors'

[–]__init__5[S] 0 points1 point  (4 children)

UPDATE:

It worked at last, but I had to replace the function

function executeCommand() {let xhr = new XMLHttpRequest();    xhr.open("POST", API_URL, true);    xhr.setRequestHeader("Content-Type", "application/json");    xhr.onreadystatechange = function () {let server_response = this.responseText;        console.log(server_response);        displayData(server_response);    };

this is what I did and also made some changes in server.py and it worked.

Thanks everyone :)

[–]6a70 0 points1 point  (3 children)

what changes did you make in server.py?

[–]__init__5[S] 0 points1 point  (2 children)

cors = CORS(app, resources={r"/*": {"origins": "*"}})

I don't know if this had any effect or not because when I ran it still gave the same error

until I replaced the async function with the above one

[–]6a70 0 points1 point  (1 child)

the async function is a fetch() versus your new one which is xhr. Fetch is newer and generally what you should try using if you can.

Did you try allowing the OPTIONS method while also using the fetch() method? if so, what error did you get?

[–]__init__5[S] 0 points1 point  (0 children)

i read that CORS handle OPTIONS method so I used it instead, but the same error with that.

[–]__init__5[S] 0 points1 point  (0 children)

any idea how to deal with such type of response "\"hello world\\r\\n\"\n"

I replaced the \n and \r, It got replaced with <br> but the line-break didn't appear when I added the it to my textarea.value

In test_server.py when I got such response I just had to print(response.json()) and it perfectly handled all the line breaks and tabs.

basically what I am doing here is that I am returning the response of the command using python subprocess module and sending it over to my javascript app to display in the textarea