我需要使用 Ajax 将array
发送到脚本 PHP 。
Javascript:
$.ajax({
type: "POST",
url: "vendas_funcoes.php",
data: {"data": arrayItens},
success: function(msg){
console.log("ok");
}
});
我已经在下面尝试使用te示例,但是它不起作用。
vendas_funcoes.php:
<?php
$data = stripslashes($_POST);
// usar foreach para ler o array
foreach($data as $d){
echo $d;
}
?>
我如何通过我的 PHP 脚本接收此array
来操纵他?显然,下面的代码将array
空发送出去。
对不起,我可能是语法错误,我是巴西人。
答案 0 :(得分:1)
您的#include <stdio.h>
union U {
int(*fnPtr)(int);
int i;
};
enum E {
OPTION_0 = 0,
OPTION_1 = 1
};
int multiply_by_two (int x) {
return 2 * x;
}
int f (int x, enum E e, union U u) {
switch (e) {
case OPTION_0:
/* Return the sum */
return x + u.i;
case OPTION_1:
/* Return 2 * x */
return u.fnPtr (x);
}
}
int main (void) {
int a;
scanf ("%d", &a);
int b = f (a, OPTION_1, &multiply_by_two);
printf ("%d\n", b);
return 0;
}
是一个对象。您需要使用$data
来获取数组。
您实际的$data["data"]
是通过POST $data
发送的。
答案 1 :(得分:0)
$_POST['data']
拥有您的数据,因为您在{p>中将密钥作为data
发送
data: {"data": arrayItens},
最好进行一些完整性检查,然后处理数据。
if(isset($_POST['data']) && !empty($_POST['data'])){
// business logic
}
答案 2 :(得分:0)
PHP将POST数据读取到$_POST
数组中,因此您可以通过读取$_POST['data']
Javascript:
$.ajax({
type: "POST",
url: "vendas_funcoes.php",
data: {"data": arrayItens},
success: function(msg){
console.log("ok");
}
});
PHP:
<?php
$data = $_POST['data'];
// usar foreach para ler o array
foreach($data as $d){
echo $d;
}
?>
提示,如果对数组执行print_r(arr)
或var_dump(arr)
,则可以轻松查看数组的结构,并找到一种有效的操作方法。