简体   繁体   English

如何将Javascript中的HTML字符串发送到Flask(python)?

[英]How to send a HTML string from Javascript to Flask (python)?

I want to send a html script to a python Flask route. 我想将html脚本发送到python Flask路由。 It can be content of an html page (with tags and text content). 它可以是html页面的内容(带有标签和文本内容)。

Here is the javascript portion: 这是JavaScript部分:

var xhr = new XMLHttpRequest();
var url = "http://localhost:5000/todo/api/v1.0/process/" + htmlstring;  
xhr.open('GET', url, false);
xhr.send();
var retrievedtext = xhr.responseText; //This will be returned by Flask

The flask portion of the code is: 该代码的烧瓶部分为:

@app.route('/todo/api/v1.0/clean/<source_code>', methods=['GET'])
def process_html_code(source_code):
    //do processing
    return result

However, I always get 404 error when sending html as it is not allowed. 但是,在发送html时,我总是会收到404错误,因为这是不允许的。 What is a good approach to be able to send html to flask? 有什么好的方法可以将html发送到flask?

You need to use the same route in your Javascript code as you defined in your Python code. 您需要在Javascript代码中使用与在Python代码中定义的相同的路由。

In your flask app you defined a route 在烧瓶应用中,您定义了一条路线

/todo/api/v1.0/clean/<source_code>

But in your Javascript code you try to send a request to 但是在您的Javascript代码中,您尝试向发送请求

http://localhost:5000/todo/api/v1.0/process/

That doesn't fit. 那不合适。 Change your url in the Javascript code according to the defined route: 根据定义的路径在Javascript代码中更改网址:

var url = "http://localhost:5000/todo/api/v1.0/clean/" + htmlstring;  

This works. 这可行。

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

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