current position:Home>Ajax sends get requests
Ajax sends get requests
2022-04-29 08:10:10【Chen Chen is trying】
demand : Click send request , Return the corresponding information to ajax in The page does not refresh
Based on the last Ajax The case of , Last case
The page is like this :【 Click send request , Display relevant data in the box 】
here js Or use of server.js
const {
response } = require('express');
// introduce express And create an application object
const express = require('express');
// Create application objects
const app = express();
// Create routing rules
//request It is the encapsulation of request message
//response It is the encapsulation of response message
app.get('/server',(request,response)=>{
// Set the response header .(' name ',' value ') Set allow cross domain
response.setHeader('Access-Control-Allow-Origin','*');
// Set the response body
response.send('HELLO AJAX');
});
// Listening port starts service
app.listen(8000,()=>{
console.log(" Service started ,8000 Port monitoring ······");
})
html It's like this in the document
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax GET request </title>
<style type="text/css">
#result{
width: 200px;
height: 100px;
border: 1px solid #90b;
}
</style>
</head>
<body>
<!-- demand : Click send request , Return the corresponding information to ajax in The page does not refresh -->
<button> Click send request </button>
<div id="result"></div>
<script>
// obtain button Elements
const btn = document.getElementsByTagName('button')[0];
const result = document.getElementById("result");
// The binding event
btn.onclick = function(){
/*Ajax operation */
//1、 Create objects
const xhr = new XMLHttpRequest();
//2. Initialization setting request method and method URL
xhr.open('GET','http://127.0.0.1:8000/server');
// Set request parameters
//xhr.open('GET','http://127.0.0.1:8000/server?a=100&b=200&c=300');
//3、 send out
xhr.send();
//4、 Event binding , Process the results returned by the server
xhr.onreadystatechange = function(){
// Judge ( The server returned all the results )
if(xhr.readyState === 4){
// Determine the response status code :200 404 403 401 500// The response status code is 2 The beginning is from success
if(xhr.status >= 200 && xhr.status < 300){
// Processing results 【 That's ok 、 head 、 Blank line 、 body 】
// obtain 、 Print response line
// console.log(xhr.status);// Status code
// console.log(xhr.statusText);// Status string
// console.log(xhr.getAllResponseHeaders);// All response headers
// console.log(xhr.response);// Response body
// Set up result The text of
result.innerHTML = xhr.response;
}else{
}
}
}
}
</script>
</body>
</html>
After running :
copyright notice
author[Chen Chen is trying],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/04/202204290810063390.html
The sidebar is recommended
- How does HTML5 display the value obtained from the database in the specified area
- Problems related to nginx rewriting interface
- What happens when you open a CSS file in eclipse without responding
- React download local template can be downloaded locally, but the deployment online download is blank
- @Babel / traverse official example running error: typeerror: traverse is not a function
- The click event of jQuery has no effect
- How to learn from non professional background?
- Usage of API documented as @since 1.8+ less... (Ctrl+F1) Inspection info: This inspection finds all
- Configuration Vue of webpack
- Introduction to nginx (learning notes)
guess what you like
Is the financial management class of qiniu school true? Can you learn how to manage money in free lessons
How does Java randomly get elements from a list
Use of Vue on in Vue
Use of Vue router
Vue basic syntax
Use of webpack
Vue diff algorithm
CSS -- Text gradient from top to bottom
Routing: Vue router
Leetcode 658. Find K closest elements
Random recommended
- How to configure Vue in Vue project config. JS to solve cross domain problems
- Centos6 makes nginx-1.21.6-rpm package -- the way to build a dream
- [vue2-sgg v] vuex
- [vue2-sgg vi] route Vue router guard
- [vue2-sgg VII] Vue export and deploy to nginx --- UI component library (element UI...)
- Chapter 12 Ajax
- Clion remote debugging ubutun server, blood lessons
- The latest vue-i18n international plug-in realizes language switching (with source code)
- Vue monitors watch usage
- Vue encapsulates Axios to the full version of the calling interface (code source code)
- Watch data monitoring in Vue and detailed explanation of various attributes in watch
- Vue encapsulates Axios to call interface Full Version (code source code) latest recommendation (II)
- Vue encapsulates Axios to the full version of the calling interface (code source code)
- Ajax usage based on JQ
- Vue project optimization
- Vue - form generator form code generation
- Data acquisition in vuex is assigned to the local problem, and when is vuex data assigned to the local problem
- The modal box component is encapsulated in Vue, and the animation effect in Vue
- Swiper, the application of swiper in Vue, and various versions of swiper are applied in Vue projects
- Python——ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted.org‘, port=443)
- Lesson 3 of ROS quick start - subscriber subscriber of ROS
- A lifeless face
- Mock in Vue JS preliminary simple use
- The Java Web servlet triggers the alert box on the front end
- CSS sets the color of the small vertical bar in front of the title
- Incomplete display of CSS background image
- [front end learning notes] save the front-end related codes
- Precautions for AWS serverless design dynamodb
- AWS serverless design - apigateway
- AWS serverless design lambda
- AWS serverless design - firewall WAF
- AWS serverless design-s3
- Python repeated element determination function program
- Nginx direction agent solves cross domain Problems-2
- The foundation of JavaScript
- DOM based on JavaScript
- Javascript based BOM
- Basic summary of JavaScript advanced
- Object oriented JavaScript
- JavaScript advanced threading mechanism and event mechanism