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

Detailed explanation of how to use the Vue license plate input component

A simple license plate input component (vue) for ...

How to use nginx to configure access to wgcloud

The nginx configuration is as follows: Such as ht...

Solution to the Docker container cannot be stopped and deleted

Find the running container id docker ps Find the ...

HTML+CSS+JS sample code to imitate the brightness adjustment effect of win10

HTML+CSS+JS imitates win10 brightness adjustment ...

Summary of commonly used tool functions in Vue projects

Table of contents Preface 1. Custom focus command...

How to uninstall Linux's native openjdk and install sun jdk

See: https://www.jb51.net/article/112612.htm Chec...

Zabbix3.4 method to monitor mongodb database status

Mongodb has a db.serverStatus() command, which ca...

Detailed explanation of setting up DNS server in Linux

1. DNS server concept Communication on the Intern...

Tutorial on logging into MySQL after installing Mysql 5.7.17

The installation of mysql-5.7.17 is introduced be...

How to deploy Node.js with Docker

Preface Node will be used as the middle layer in ...

Detailed explanation of views in MySQL

view: Views in MySQL have many similarities with ...

How to expand the disk size of a virtual machine

After Vmvare sets the disk size of the virtual ma...

Two ways to implement square div using CSS

Goal: Create a square whose side length is equal ...

What is WML?

WML (Wireless Markup Language). It is a markup la...