Home Web Front-end JS Tutorial Detailed explanation of the method of using ajax to pass arrays and receive in the background

Detailed explanation of the method of using ajax to pass arrays and receive in the background

May 22, 2018 am 11:52 AM
ajax Backstage array

This article mainly introduces to you the relevant information about using ajax to transfer arrays and receive in the background. The introduction in the article is very detailed and has certain reference and learning value for everyone. Friends who need it can come and learn together.

Preface

We are using ajax to asynchronously submit the multi-select box to get the ID of the object that needs to be operated. At this time, we can put each Make an object with the id, then put it into an array, and then use JSON.stringify() to format the array as json; parse our json string in the inputStream in the background, and then Just use:

new JSONArray() Get the json array, and loop to parse the attributes we want:

var countsCheckBox = $("input[type='checkbox']:checked"); 
 var booksid = []; 
 for(var i=0;i<countsCheckBox.length;i++){ 
 //使用[]取得元素是是一个domElement元素,取值需要使用.value, 
 //如果使用countsCheckBox.eq(i) 则是一个Obkject元素,就可以使用val()取值 
 //alert(countsCheckBox[i].value); 
 mysendbook_id = {}; 
 mysendbook_id['book_id'] = countsCheckBox[i].value; 
 booksid[i] = mysendbook_id; 
 } 
 //alert(booksid); 
  var confirmdel= confirm('确认要删除吗?'); 
  if(confirmdel){ 
  //开始请求删除 
   $.ajax({ 
     url:'selectdelbooks', 
     data:JSON.stringify(booksid), 
     type:'post', 
     success:function(res){ 
      alert("删除成功"); 
     location.replace("/TheDemo/books/pageBooksShow"); 
     } 
     }); 
  }
Copy after login

In the above js, we put each selected id into the "book_id" attribute of mysendbook_id, and then put this object into the booksid array; use

# when sending an asynchronous request ##JSON.stringify(bookid)Format this booksid array and get a json array.

Let’s look at how we receive it in the background:

One is to make a list in a class, and this list contains only one attribute: The class of bookid is added to this formal parameter using the annotation

@RequestBody. But this is more troublesome;

Another way is to get data from the input stream, use

IOUtils.toString to convert the inputStream to a string, and then use new JSONArray( mybooksid);Get this json array

To get the attribute value of book_id in each json

<span style="font-family:SimSun;font-size: 10.5pt;"> </span><span style="font-family:KaiTi_GB2312;font-size:14px;"> @RequestMapping("selectdelbooks") 
 public String selectdelbooks(HttpServletRequest request) throws Exception { 
  ServletInputStream inputStream = request.getInputStream(); 
  String mybooksid = IOUtils.toString(inputStream); 
  JSONArray jsonarr = new JSONArray(mybooksid); 
  List<String> book_id =new ArrayList<String>(); 
  for (int i=0;i<jsonarr.length();i++){ 
   book_id.add(((JSONObject)jsonarr.get(i)).getString("book_id")); 
  }...</span>
Copy after login

This way We get a list with the id value we selected.

Information in the database:


Multiple selection on the page:

obtained in the background The id of the selected book:


In this way, you can get the set of ids, and the subsequent operations will be convenient.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to solve the problem of Ajax passing data with special characters

Configuring Chrome to support local (file protocol) AJAX request (graphic tutorial)

Simple implementation of AJAX paging effect (graphic tutorial)

The above is the detailed content of Detailed explanation of the method of using ajax to pass arrays and receive in the background. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

How to remove duplicate elements from PHP array using foreach loop?

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

PHP array key value flipping: Comparative performance analysis of different methods

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

PHP array multi-dimensional sorting practice: from simple to complex scenarios

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

Application of PHP array grouping function in data sorting

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

PHP vs. Ajax: Solutions for creating dynamically loaded content

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

The role of PHP array grouping function in finding duplicate elements

See all articles