Detailed examples of ajax usage in js and jQuery

Detailed examples of ajax usage in js and jQuery

Native JS

How to send a get request

  • Create an ajax object
    • var xhr = new XMLHttpRequest()
  • Set the request method and request address [, whether asynchronous]
    • xhr.open('get', '/ajax'[, true or false])
  • Prepare to receive request body
    • xhr.onload = function () { console.log(xhr.responseText) }
    • xhr.onreadystatechange = function () { if (xhr.readyState === 4) { console.log( xhr.responseText ) } }
  • Send Request
    • xhr.send(null)
var xhr = new XMLHttpRequest()
xhr.open('get', '/ajax')
xhr.onload = function () {
  console.log(xhr.responseText)
}
xhr.send(null)

How to send a post request

  • Create an ajax object
    • var xhr = new XMLHttpRequest()
  • Set the request method and request address [, whether asynchronous]
    • xhr.open('post', '/ajax'[, true or false])
  • Prepare to receive request body
    • xhr.onload = function () { console.log(xhr.responseText) }
    • xhr.onreadystatechange = function () { if (xhr.readyState === 4) { console.log( xhr.responseText ) } }
  • Send Request
    • xhr.send(null)
var xhr = new XMLHttpRequest()
xhr.open('post', '/ajax')
xhr.onload = function () {
  console.log(xhr.responseText)
}
xhr.send(null)

Send a get request with parameters

  • var xhr = new XMLHttpRequest
  • Concatenate parameters directly after the request address, starting with ?, in the form of key=value, and separate multiple parameters with &
    • xhr.open('get', '/ajax?name=Jack&age=18')
  • xhr.onload = function () { console.log( xhr.responseText ) }
  • xhr.send()

Send a post request with parameters

var xhr = new XMLHttpRequest

No need to add anything after the request address

  • xhr.open('post', '/ajax')

xhr.onload = function () { console.log( xhr.responseText ) }

The parameters carried by the post method are written directly in the () after xhr.send()

  • Collect data key=value by yourself
    • Set the request header yourself
    • xhr.setRequestHeadr('content-type', 'application/x-www-form-urlencoded')
  • FormData collects data
    • Nothing is needed, just use FormData to collect data
    • var fd = new FormData(DOM)
    • When sending a request, just bring fd over.
var xhr = new XMLHttpRequest()
xhr.open('post', '/ajax')
xhr.onload = function () {
  console.log(xhr.responseText)
}
xhr.setRequestHeadr('content-type', 'application/x-www-form-urlencoded')
xhr.send('key=value&key=value')
var fd = new FormData(document.querySelector('form'))
var xhr = new XMLHttpRequest()
xhr.open('post', '/ajax')
xhr.onload = function () {
  console.log(xhr.responseText)
}
xhr.send(fd)

jQuery

How to use several parameters of $.get

address

  • Parameter key=value or { name: 'Jack' }
  • Success callback function
  • The expected data type returned by the backend
    • text : Do nothing and give you the result directly
    • json: will definitely execute a JSON.parse() step

$.post several parameters, how to use them

  • address
  • Parameter key=value or { name: 'Jack' }, FormData cannot be sent
  • Success callback function
  • The expected data type returned by the backend

$.ajax several parameters, how to use

  • It is the configuration item options
    • url: request address
    • method/type: request method
    • data: carries parameters
    • dataType: The data type returned by the backend
    • success: successful callback
    • error: Failed callback
    • contentType: used when sending FormData
    • processData: used when sending FormData

JSONP

How to send jaonp request using $.ajax

  • dataType must be jsonp
  • The method must be get
  • jsonp: Determined by the background
$.ajax({
  url: '/jsonp',
  data: {},
  dataType: 'jsonp',
  jsonp: 'callback',
  success (res) {
    console.log(res)
  }
})

Summarize

This is the end of this article about the usage of ajax in js and jQuery. For more information about the usage of ajax in js and jQuery, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Method to request local json file through jquery ajax
  • How to terminate the ajax request being sent by js and jQuery
  • Jquery returns json data example via ajax request to NodeJS
  • jQuery tutorial ajax request json data example
  • A brief analysis of ajax request json data and parsing it with js (example analysis)
  • js implements an ajax request every 5 minutes
  • JS interception global ajax request example analysis
  • Javascript sends AJAX request example code
  • How to prevent duplicate submission of JS Ajax requests

<<:  CentOs7 64-bit MySQL 5.6.40 source code installation process

>>:  Nginx defines domain name access method

Recommend

Introduction to encryption of grub boot program in Linux

Table of contents 1. What is grub encryption 2. g...

Detailed explanation of vue-router 4 usage examples

Table of contents 1. Install and create an instan...

Browser compatibility summary of common CSS properties (recommended)

Why do we need to summarize the browser compatibi...

A brief discussion on the font settings in web pages

Setting the font for the entire site has always b...

Several ways to add timestamps in MySQL tables

Scenario: The data in a table needs to be synchro...

How to make your JavaScript functions more elegant

Table of contents Object parameters using destruc...

Detailed explanation of Linux DMA interface knowledge points

1. Two types of DMA mapping 1.1. Consistent DMA m...

Mysql case analysis of transaction isolation level

Table of contents 1. Theory SERIALIZABLE REPEATAB...

XHTML no longer uses some obsolete elements in HTML

When we do CSS web page layout, we all know that i...

Detailed example of locating and optimizing slow query sql in MySQL

Table of contents 1. How to locate and optimize s...

A brief discussion on the magic of parseInt() in JavaScript

cause The reason for writing this blog is that I ...

How to deploy Redis 6.x cluster through Docker

System environment: Redis version: 6.0.8 Docker v...

HTML tag ID can be a variable

<table id=" <%=var1%>">, the...