Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use jQuery AJAX to transmit data in the background

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use jQuery AJAX to achieve background data transmission related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this article on how to use jQuery AJAX to achieve background data transmission will have a harvest, let's take a look at it.

1. The background returns a page

Js code

$.ajax ({

Async: false

Cache: false

Type: 'post'

Url: 'area/preparecreate'

Error: function () {

Alert ('smx failed')

}

Success: function (data) {

$("# content-wrapper") .html (data)

}

})

Java code

@ requestmapping (value = "area/preparecreate", method = requestmethod.post)

Public string editareawithfile () {

Return "area/create

/ / return "error"

}

two。 The backend returns a basic type, string,long, etc.

Js code

$.ajax ({

Async: false

Cache: false

Type: 'post'

Url: 'app/area/delete'

Datatype: "json"

Error: function () {

Alert ('smx failed')

}

Success: function (data) {

Alert (data)

}

})

Java code

@ responsebody

@ requestmapping (value = "area/delete", method = requestmethod.post)

Public long editarea () {

/ / processing parameters

Return 3

}

3. The background returns an entity class

Step 1: define an entity class

Public class ajaxresponsemsg {

Private int flag

Private string msg

}

Step 2: js code

$.ajax ({

Async: false

Cache: false

Type: 'post'

Url: 'app/area/delete'

Error: function () {

Alert ('smx failed')

}

Success: function (data) {

Var jsondata = json.parse (data)

If (jsondata .flag = = 0) {/ / request succeeded

Alert ("background operation results")

} else {

Alert (jsondata .msg)

}

}

})

Step 3: java code

@ responsebody

@ requestmapping (value = "area/delete", method = requestmethod.post)

Public ajaxresponsemsg editareawithfile () {

/ / do the operation

Boolean result = true

Ajaxresponsemsg ajaxmsg = new ajaxresponsemsg ()

If (result = = true)

Ajaxmsg.setflag (0)

Else {

Ajaxmsg.setflag (1)

Ajaxmsg.setmsg ("cannot add duplicate devices to a sensor")

}

Return ajaxmsg; / / returns an entity class to the foreground, including whether the operation was successful and the reason for the failure

}

4. The background returns an entity class list (the fields of the entity class are all basic types)

Entity class

Public class section {

Private long id; / / id

Private string name; / / name

}

Front Desk

$.ajax ({

Async: false

Cache: false

Type: 'post'

Url: "section/getsections"

Error: function () {

Alert (failure)

}

Success: function (data) {

Var jsondata = json.parse (data); / / jsondata is all the intervals under this downlink (json format)

For (var I = 0; I < jsondata.length; iTunes +) {

Alert (jsondata [I] .id)

Alert (jsondata [I] .name)

}

}

})

Backstage

@ responsebody

@ requestmapping ("/ section/getsections")

@ requirespermissions ("section:view")

Public list getsections () {

List sections = new list ()

Return sections

}

5. The background returns an entity class list (the fields of the entity class include the list type)

Step 1: define the entity class chartseries

Public class chartseries {

Private string name; / / Curve name

Ordinate of private list data; / / curve

Abscissa of private list occurtime; / / curve

/ / add get set method

}

Step 2: js code

$.ajax ({

Url: "/ datarecord/chart", / / get the data column

Type: 'get'

Data: {

}

Success: function (data) {

Formatstringtojson (data); / / A pair of data processing

}

})

Function formatstringtojson (data) {

Jsondata = json.parse (data); / / transfer the data from the background to json format

/ / jsondata is a two-dimensional array, because the field data of the entity class chartseries is also a list

For (x in jsondata) {/ / traverses the array in json format to fetch elements, with x representing the subscript

For (y in jsondata [x] .data) {

Alert (jsondata [x] .occurtime [y]); / / get the

Alert (jsondata [x] .data [y])

}

}

}

Step 3: java code

@ responsebody

@ requirespermissions ("datarecord:view")

@ requestmapping (value = "/ datarecord/chart}", method = requestmethod.get)

Public list getchart () {

List list = new list ()

/ / assign a value to list

Return list; / / list

}

This is the end of the article on "how to use jQuery AJAX to transmit data in the background". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use jQuery AJAX to transmit data in the background". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report