如何获取要在python fi中使用的JS变量

2024-04-25 00:52:21 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好。我需要帮助从JS获取变量,以便在python代码中使用。我已经浏览了google的所有地方,并尝试过使用AJAX和JQuery,但没有任何运气。我可能只是做得不对。你知道吗

我的目标是能够使用HTML5地理定位的准确性和使用坐标在我的python代码。如果有人能帮我,那就太棒了。谢谢。你知道吗

Python Code

from flask import *
from flask_jsglue import JSGlue
app = Flask(__name__)
jsglue = JSGlue(app)
import json

@app.route('/')
def index():
    return render_template('the_new_html.html')

@app.route('/receiver', methods=['GET', 'POST'])
def worker():
    lat = request.form['lat']
    long = request.form['long']
    print(lat)
    print(long)






if __name__ == '__main__':
    app.run(debug=True)

HTML file

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Geo</title>
    <script>
      window.onload = function()
      {

        init();
      }

      function init()
      {
        navigator.geolocation.getCurrentPosition(positionsuccess,
        positionerror);
      }

      function positionsuccess(position)
      {
        var lat = position.coords.latitude;
        var long = position.coords.longitude;
        document.getElementById('lat').innerHTML = lat;
        document.getElementById('long').innerHTML = long;
        event.preventDefault();
      }
      function positionerror(error)
      {
        alert(error.message);
      }


    </script>
    <script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript">
     </script>
  </head>
  <body>
    <form>
      Lat: <span id='lat'></span>
      Long: <span id='long'></span>
      <button type="submit" name="button">Button</button>
    </form>
    <script>

    </script>

  </body>
</html>

Tags: 代码namefromimportformappflaskhtml
1条回答
网友
1楼 · 发布于 2024-04-25 00:52:21
   <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Geo</title>
        <script>
          window.onload = function()
          {

            init();
          }

          function init()
          {
            navigator.geolocation.getCurrentPosition(positionsuccess,
            positionerror);
          }

          function positionsuccess(position)
          {
            var lat = position.coords.latitude;
            var long = position.coords.longitude;
            document.getElementById('lat').innerHTML = lat;
            document.getElementById('long').innerHTML = long;
            event.preventDefault();
          }
          function positionerror(error)
          {
            alert(error.message);
          }

          function buttonClickHandler() {
            const lat = document.getElementById('lat').textContent;
            const long = document.getElementById('long').textContent;
            fetch('/receiver', , {
               method: 'post',
               headers: {
                  'Accept': 'application/json, text/plain, */*',
                  'Content-Type': 'application/json'
               },
              body: JSON.stringify({lat, long})
               }).then(res=>res.json())
              .then(res => console.log(res));
          }

        </script>
        <script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script type="text/javascript">
         </script>
      </head>
      <body>
        <form>
          Lat: <span id='lat'></span>
          Long: <span id='long'></span>
          <button type="button" name="button" onclick="buttonClickHandler">Button</button>
        </form>
        <script>

        </script>

      </body>
    </html>

在Flask服务器上,您可以在以下位置获取数据:

@app.route('/receiver', methods=['GET', 'POST'])
def worker():
    data= request.data
    print(data)

相关问题 更多 >