


Native JS implements Ajax cross-domain request flask response content (graphic tutorial)
May 22, 2018 am 09:37 AMThis article mainly introduces the JS implementation of Ajax cross-domain request flask response content in detail. It has a certain reference value. Interested friends can refer to it
The Ajax method is good and the website feels like It's high-end, but due to the limitations of Js, cross-domain Ajax cannot be implemented. Here, let's talk about the solution. The premise is that you need to be able to control the response on the flask side.
Main technology:
Modify the corresponding header of the server so that it can correspond to any domain name. and sets the response header so that it can correspond to the POST method.
Implementation code:
Put the flask code here first:
from flask import make_response @app.route('/test',methods=['get','post']) def Test(): if request.method=='GET': rst = make_response('aaa') rst.headers['Access-Control-Allow-Origin'] = '*' #任意域名 return rst else: rst = make_response('bbb') rst.headers['Access-Control-Allow-Origin'] = '*' rst.headers['Access-Control-Allow-Methods'] = 'POST' #响应POST return rst
html test code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <span id="ss">test get</span> <button onclick="getAjax()">click</button> <p id="time">test post</p> <input type="submit" value="click" onclick="getPostAjax()"> <script> function getPostAjax() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function () { if(xmlhttp.readyState=4 && xmlhttp.status ==200 ) { document.getElementById("time").innerText = xmlhttp.responseText; } } xmlhttp.open("POST","http://localhost:5000/test",true); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); #这句话可以发送post数据,没有此句post的内容无法传递 xmlhttp.send(); } function getAjax() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function () { if(xmlhttp.readyState==4 && xmlhttp.status == 200){ document.getElementById("ss").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","http://localhost:5000/test",true); xmlhttp.send(); } </script> </body> </html>
Unable to control the response header
For In this case, the get request can be completed using jquery, but there is nothing you can do about post. Currently, I am writing both the front and back ends, so I will not consider this situation for the time being.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
jQuery ajaxReading json and sorting method detailed explanation
jQuery Ajax verification user name
The above is the detailed content of Native JS implements Ajax cross-domain request flask response content (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Start from scratch and guide you step by step to install Flask and quickly establish a personal blog

Guide to installing the Flask framework: Detailed steps to help you install Flask correctly

How to solve the 403 error encountered by jQuery AJAX request

How to solve jQuery AJAX request 403 error

How to get variables from PHP method using Ajax?

Flask installation and configuration tutorial: a tool to easily build Python web applications

How to quickly deploy Flask applications

How to solve the problem of jQuery AJAX error 403?
