[英]$.post - array from javascript to php
我在javascript中有這個數組:
arr = [1, "string", 3]
這就是我的ajax調用:
$.post("./ajax_book_client.php",
{'client_info[]': arr},
function(data) {
// some stuffs here
}
});
這是PHP摘錄:
<?php
$arr = $_POST['client_info'];
// I want to access the array, indexed, like this:
$arr[0] = 2;
$arr[1] = "Hello";
$arr[2] = 10000;
?>
但是我得到這個錯誤:
未捕獲的TypeError:非法調用jquery-1.8.3.min.js:2
正確的方法是什么? 謝謝!
除去}
從});
刪除語法錯誤。
另外,無需在client_info
使用[]
,因此可以將其刪除。
采用:
<script>
var arr = [1, "string", 3];
$.post("./ajax_book_client.php",
{'client_info': arr},
function(data) {
// some stuffs here
}
);
</script>
ajax_book_client.php
<?php
$arr = $_POST['client_info'];
echo $arr[0];
echo $arr[1];
echo $arr[2];
?>
大括號,更改:
$.post("./ajax_book_client.php",
{'client_info[]': arr},
function(data) {
// some stuffs here
}
});
至
$.post("./ajax_book_client.php",
{'client_info[]': arr},
function(data) {
// some stuffs here
// } <--remove this
});
您的JS中存在語法錯誤。 刪除一個}
。
將client_info[]
移至client_info
並大括號
嘗試
<script>
var arr = [1, "string", 3];
$.post("./ajax_book_client.php",
{'client_info': arr},
function(data) {
// some stuffs here
}
);
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.