使用XMLHttpRequest发送POST请求
以下示例通过向后端发送POST请求,接收后端返回的信息
1 2 3 4 5 6 7 8 9 10 11
| function send_request(){ const xhr = new XMLHttpRequest(); xhr.open('POST','http://localhost:8080/route','true'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function () { if(xhr.readyState === 4 && xhr.status === 200){ console.log(JSON.parse(xhr.responseText)) } } }
|
以flask管理后端,向前端返回一个url
1 2 3 4 5 6 7 8 9 10
| from flask import Flask from flask_cors import CORS
app = Flask(__name__)
CORS(app, supports_credentials=True)
@app.route('/route', methods=['POST', 'GET']) def get_url(url): return jsonify(file_url=url)
|