current position:Home>Ajax sends get requests

Ajax sends get requests

2022-04-29 08:10:10Chen 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 】

 Insert picture description here
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 :
 Insert picture description here
 Insert picture description here

 Insert picture description here

copyright notice
author[Chen Chen is trying],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/04/202204290810063390.html

Random recommended