-1

I made my rest api in python and now I want to test it on client part.I'm not really good in javascript but I wrote this code, that makes api requst when the button was cllicked. But, when I press the button, app shows me this error:

Access to XMLHttpRequest at 'http://127.0.0.1:8000/users' from origin 'http://localhos 
http://gc.kis.v2.scr.kaspersky-labs.com/FD126C42-EBFA-4E12-B309-BB3FDD723AC1/main.js?attr=LKuZpXi47SzFPwXe5haD7c5-HMZPTa_1fbTwFjACCKHXaOAG_igEIGZMA7ya-2CbBCawsigupM7LVe_XuHMDjY_-yDDSKPwyPbH3vXr_fsawEPTC20m4ajd1lSMjtd7GFfmBOjgzS8Q0S6XFanyinou9DV8lOZWR7jIzZ8bkIE4 
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I know the problem can be very silly, but if you know what am I doing wrong, please tell me . I'd really appreciate it!

Here's my code: html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
    <button>Press to get all users</button>
    <div>
        <H1>hello!</H1>
    </div>
    <script src="scripts.js">
    </script>
</body>
</html>

script.js:

var btn = document.querySelector('button');
url = 'http://127.0.0.1:8000/users';
btn.onclick = function httpGet(){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", url, false);
    xmlHttp.send(null);
    console.log(xmlHttp.responseText);
};
Cross
  • 497
  • 1
  • 3
  • 13

1 Answers1

0

You need to enable CORS on server side, basically add Access-Control-Allow-Origin on python server code, your client code is correct, just enable CORS on server side

TechnoTech
  • 744
  • 4
  • 14