Home > Web Front-end > JS Tutorial > How to solve the problem of jQuery.ajax passing arrays to the background

How to solve the problem of jQuery.ajax passing arrays to the background

小云云
Release: 2018-05-16 11:20:56
Original
2673 people have browsed it

This article mainly introduces in detail the solution to the problem of jQuery.ajax passing arrays to the background. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

Today I reviewed a problem. jQuery.ajax passes an array to the background, but the value cannot be received in the background.

Part of the front-end js method code is as follows:

//创建一个测试数组
var boxIds = new Array();
boxIds.push(12182);
boxIds.push(12183);
boxIds.push(12184);
//向后台交互
$.ajax({
  url: "/xxx",
  type: "GET",
  data: {
    "boxIds": boxIds,
    "boxType": 0,
    "time": new Date().getTime()
  },
  success: function(data) {
    //do sth...
  }
});
Copy after login

Backend controller code (SpringMVC)

@ResponseBody
@RequestMapping(value = "/box/changeLock")
public String changeLock(final Long[] boxIds, final int boxType) {
  return locker_ChangeLockService.changeLock(boxIds, boxType);
}
Copy after login

Observe the request sent in the browser, you can find that the parameters are as follows:

You can see that the name of the parameter is boxIds[] instead of the boxIds we defined. That is to say, when passing an array, "" will be automatically added after our array name. []", so the background cannot receive it.

Solution:

Set the tradional attribute of jQuery.ajax

$.ajax({
  url: "/xxx",
  type: "GET",
  data: {
    "boxIds": boxIds,
    "boxType": 0,
    "time": new Date().getTime()
  },
  traditional: true,//这里设置为true
  success: function(data) {
    //do sth...
  }
});
Copy after login

Reissue the request after modification, Observe the browser:

Request Parameter 2

This time the parameter does not contain "[]", and the array can be successfully received in the background .

Related recommendations:

Detailed example of Ajax passing array parameter value to the server

How to use php to pass an array to js Script

jquery ajax fails to pass array to the background

The above is the detailed content of How to solve the problem of jQuery.ajax passing arrays to the background. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template