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)

May 22, 2018 am 09:37 AM
ajax flask javascript

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Start from scratch and guide you step by step to install Flask and quickly establish a personal blog Start from scratch and guide you step by step to install Flask and quickly establish a personal blog Feb 19, 2024 pm 04:01 PM

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 Guide to installing the Flask framework: Detailed steps to help you install Flask correctly Feb 18, 2024 pm 10:51 PM

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 the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

How to solve the 403 error encountered by jQuery AJAX request

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

How to solve jQuery AJAX request 403 error

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

How to get variables from PHP method using Ajax?

Flask installation and configuration tutorial: a tool to easily build Python web applications Flask installation and configuration tutorial: a tool to easily build Python web applications Feb 20, 2024 pm 11:12 PM

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

How to quickly deploy Flask applications How to quickly deploy Flask applications Jan 19, 2024 am 10:26 AM

How to quickly deploy Flask applications

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQuery AJAX error 403?

See all articles