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

How to install nginx in docker and configure access via https

1. Download the latest nginx docker image $ docke...

Detailed steps to install the NERDTree plugin in Vim on Ubuntu

NERDTree is a file system browser for Vim. With t...

21 MySQL standardization and optimization best practices!

Preface Every good habit is a treasure. This arti...

MySQL Innodb key features insert buffer

Table of contents What is insert buffer? What are...

Embed codes for several older players

The players we see on the web pages are nothing m...

Implementation ideas for docker registry image synchronization

Intro Previously, our docker images were stored i...

MySQL optimization: use join instead of subquery

Use JOIN instead of sub-queries MySQL supports SQ...

How to create a project with WeChat Mini Program using typescript

Create a project Create a project in WeChat Devel...

MySQL joint table query basic operation left-join common pitfalls

Overview For small and medium-sized projects, joi...

A quick solution to the first login failure in mysql5.7.20

First, we will introduce how (1) MySQL 5.7 has a ...

JavaScript to achieve the effect of clicking on the submenu

This article shares the specific code of JavaScri...

How to sort a row or column in mysql

method: By desc: Neither can be achieved: Method ...

JavaScript to add and delete messages on the message board

This article shares a small example of adding and...

Vue realizes adding watermark to uploaded pictures (upgraded version)

The vue project implements an upgraded version of...