API-вызов из javascript в флягу получить Ошибка загрузки - PullRequest
0 голосов
/ 30 августа 2018

В основном мне нужно выполнить вызов API между Java-скриптом и Python-флаконом, но у меня возникла проблема с ответом при выполнении этого кода.

Во время выполнения этого кода на Python я получил ответ в инструменте Postman. Но это не работает в JavaScript.

Как я могу решить это .....


Это мой файл JS


<html>
<head>
<style type="text/css">
#container{
    min-width: 420px;
    max-width: 650px;
    margin: 0 auto;
    }
</style>
    <script 
   src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
  </script>
</head>
<body>
    <div id="container"></div>
    <script>
    var request = new XMLHttpRequest();
    request.open('GET', 'https://Ip:port/v1/user', true);
    request.onload = function () {
    // Begin accessing JSON data here
    var data = JSON.parse(this.response);
    if (request.status >= 200 && request.status < 400) {
        console.log("Success");
    } else {
        console.log("Failure");
    }
}
request.send();
    </script>
</body>
</html>

This is my Python File

from flask import Flask
app = Flask(__name__)
import pandas as pd
from flask import Response
import json
@app.route('/v1/user',methods=['GET'])
def hello_world():
  data = pd.read_csv("C:/xampp/htdocs/csvfiles/sankey_1.csv")
  df=pd.DataFrame(data)
  a=[]
  b={'pdx':[],'drg':[],'pprox':[]}
  for index, row in df.iterrows():
    #print (type(row[1]),type(row[2]),type(str(row[3])))
    result= ({"pdx":row[1], "drg":row[2],"pproc":str(row[3])})
    a.append(result)
  print (type(json.dumps(a)))
  return Response(json.dumps(a))        
if __name__ == '__main__':
  app.run(host='IP', port=5000, debug=True, threaded=True)

ISSUE:This is the issue i get while run this code i don't know why happen

 Failed to load http://IP:port/v1/user:No'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://IP comes here' is therefore not allowed access.
...