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 
    // Set the response header .(' name ',' value ')  Set allow cross domain 

    // Set the response body 
    response.send('HELLO AJAX');
// Listening port starts service 
    console.log(" Service started ,8000 Port monitoring ······");

html It's like this in the document

<!DOCTYPE html>
<html lang="en">
    <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">
            width: 200px;
            height: 100px;
            border: 1px solid #90b;
    <!--  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>
        //  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
            // Set request parameters  
            //3、 send out 
            //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;



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.

Random recommended