Ajax和php-将javascript数组发送到php
我试图将数据发送到我的php脚本,但我想我错过了一些东西 首先,Ajax和php-将javascript数组发送到php,php,ajax,Php,Ajax,我试图将数据发送到我的php脚本,但我想我错过了一些东西 首先, function deleteData2() { var artistIds = new Array(); $(".p16 input:checked").each(function(){ artistIds.push($(this).attr('id')); }); $.post('/json/crewonly/delete
function deleteData2()
{
var artistIds = new Array();
$(".p16 input:checked").each(function(){
artistIds.push($(this).attr('id'));
});
$.post('/json/crewonly/deleteDataAjax2', JSON.stringify({'artistIds': artistIds}),function(response){
if(response=='ok')
alert(artistIds);
});
}
上面的代码是我的js文件。我在var artistIds中有artistIds。我的目标是将这个数组发送到我的php脚本中。为此,我将其设置为json,我的意思是用json.stringify对其进行编码
然后在php端,我使用下面的代码。原因可能是什么
public function deleteDataAjax2() {
$array=json_decode($_POST['artistIds']);
if (isset($array))
$this->sendJSONResponse('ok');
}
您正在以JSON原始字符串的形式传递数据,但您的PHP正在尝试通过将数据解析为
application/x-www-form-urlencoded
,然后查看artistIds
键来查找该字符串
假设数组是平面的:忘记JSON。你不需要它
$.post('/json/crewonly/deleteDataAjax2', {'artistIds': artistIds},function(response){
以及:
如果阵列不是平坦的,则:
$.post('/json/crewonly/deleteDataAjax2',
{ json: JSON.stringify({'artistIds': artistIds}) },
function(response){
和(添加了适当的错误检查):
您正在以JSON原始字符串的形式传递数据,但您的PHP正在尝试通过将数据解析为
application/x-www-form-urlencoded
,然后查看artistIds
键来查找该字符串
假设数组是平面的:忘记JSON。你不需要它
$.post('/json/crewonly/deleteDataAjax2', {'artistIds': artistIds},function(response){
以及:
如果阵列不是平坦的,则:
$.post('/json/crewonly/deleteDataAjax2',
{ json: JSON.stringify({'artistIds': artistIds}) },
function(response){
和(添加了适当的错误检查):
您是否尝试过打印($\u POST)?在这种情况下,这应该是第一步。对不起,我不相信你
$\u POST
始终是一个数组,即使未发送POST数据。(不要打印$array
,而是打印整个$\u POST
变量。)您是否尝试过打印($\u POST)
?在这种情况下,这应该是第一步。对不起,我不相信你$\u POST
始终是一个数组,即使未发送POST数据。(不要打印$array
,而是打印整个$\u POST
变量。)