简体   繁体   English

如何使用 JS 将 XML 数据发送到 Flask 后端

[英]How to send XML data to Flask backend using JS

I want to send some part of a webpage as an XML document to my Flask backend using JS:我想使用 JS 将网页的某些部分作为 XML 文档发送到我的 Flask 后端:

I've tried sending it using this JavaScript code:我尝试使用此 JavaScript 代码发送它:

  function sendData() {
    let data = document.getElementById("tableID");
    console.log(data);
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "app.py", true);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(JSON.stringify(data));
    console.log(xhr);
  }

but didn't get a response from the backend:但没有得到后端的响应:

@app.route('/validate', methods=['POST', 'GET'])
def validate():
    tracks = observed_tracks(get_tracks())
    if request.method == "POST":
        print("submitted......")
    return render_template("tools/validate.html", tracks=tracks)

The data sent looks like this (this is just a sample, the data is dynamically generated):发送的数据是这样的(这只是一个示例,数据是动态生成的):

<table id="tableID">
    <tbody>
    <tr class="adminToolsLine">
          <td class="adminToolsBox1" type="text" name="Artist" id="Artist"> Artist02 </td>
          <td class="adminToolsBox1" type="text" name="Album" id="Album">ALbum f Artist02 </td>
          <td class="adminToolsBox2" type="text" name="Track" id="Track"> ihnj </td>
          <td class="adminToolsBox3" type="text" name="Genre" id="Genre"> Indie </td>
          <td class="adminToolsBox4" type="text" name="Length" id="Length"> 9:32 </td>
          <td class="adminToolsBox1" type="text" name="Infos" id="Infos"> injojn k </td>
          <td class="adminToolsBox0Line" onclick="removeParent(this)">X</td>
        </tr>
      <tr class="adminToolsLine">
          <td class="adminToolsBox1" type="text" name="Artist" id="Artist"> testgenreMenu </td>
          <td class="adminToolsBox1" type="text" name="Album" id="Album"> testgenreMenu </td>
          <td class="adminToolsBox2" type="text" name="Track" id="Track">track01_testgenre </td>
          <td class="adminToolsBox3" type="text" name="Genre" id="Genre"> Folk </td>
          <td class="adminToolsBox4" type="text" name="Length" id="Length"> 02:00 </td>
          <td class="adminToolsBox1" type="text" name="Infos" id="Infos"> shouldbefolk </td>
          <td class="adminToolsBox0Line" onclick="removeParent(this)">X</td>
        </tr>
  </tbody>
</table>

Do you have any idea of what should I change to the back end or to the JS to make it work?您知道我应该对后端或 JS 进行什么更改以使其正常工作吗?

In your JS function, you are trying to send a POST request to a python file (app.py), but what you need to do is send a POST request to a route - a flask route that you have in your app.py file.在您的 JS function 中,您正尝试将 POST 请求发送到 python 文件(app.py),但您需要做的是将 POST 请求发送到路由 - 您的 app.pyD 文件中的 Z319C3206A7F10C17C3BZ9116 路由. So you need to specify the desired route as a parameter in your POST request.因此,您需要在 POST 请求中指定所需的路由作为参数。

If you check the documentation for XMLHttpRequest() , you will see that the the xhr.open() method takes "URL" is a parameter.如果您查看XMLHttpRequest()的文档,您会看到 xhr.open() 方法采用“URL”作为参数。

You haven't provided a URL, you provided a python file.您没有提供 URL,您提供了 python 文件。

So,所以,

xhr.open("POST", "app.py", true);

should be应该

xhr.open("POST", "/validate", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM