Home > Web Front-end > JS Tutorial > Native JS implements Ajax cross-domain request flask response content (graphic tutorial)

Native JS implements Ajax cross-domain request flask response content (graphic tutorial)

亚连
Release: 2018-05-22 09:37:59
Original
1618 people have browsed it

This 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
Copy after login

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>
Copy after login

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

Explanation of XML in AJAX

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template