如何通过JQuery Post传递Javascript数组,以便通过PHP $ _POST数组访问其所有内容?

Ple*_*pMe 40 javascript php jquery

如何通过JQuery Post传递Javascript数组,以便通过PHP $ _POST数组访问其所有内容?

请展示一个可以解决问题的代码示例.

谢谢!

pro*_*son 59

如果你想传递一个JavaScript对象/哈希(即PHP中的一个关联数组),你会这样做:

$.post('/url/to/page', {'key1': 'value', 'key2': 'value'});
Run Code Online (Sandbox Code Playgroud)

如果你想传递一个实际的数组(即PHP中的索引数组),那么你可以这样做:

$.post('/url/to/page', {'someKeyName': ['value','value']});
Run Code Online (Sandbox Code Playgroud)

如果要传递JavaScript数组,则可以执行以下操作:

$.post('/url/to/page', {'someKeyName': variableName});
Run Code Online (Sandbox Code Playgroud)


Gro*_*ain 23

这非常简单.在你的JS中,你要做的就是这个或类似的东西:

var array = ["thing1", "thing2", "thing3"];

var parameters = {
  "array1[]": array,
  ...
};

$.post(
  'your/page.php',
  parameters
)
.done(function(data, statusText) {
    // This block is optional, fires when the ajax call is complete
});
Run Code Online (Sandbox Code Playgroud)

在您的php页面中,数组形式的值将通过$_POST['array1'].

引用


Rui*_*Rui 8

这是一个例子:

$.post("test.php", { 'choices[]': ["Jon", "Susan"] });
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • var choices = ["Jon","Susan"]; $ .post("test.php",{'choices []':choices}); (6认同)