gpt4 book ai didi

javascript - 无法从 jquery 访问数组

转载 作者:搜寻专家 更新时间:2023-10-31 21:24:06 24 4
gpt4 key购买 nike

好的,在我上一个问题之前,我已经尝试对我的代码进行一些修改,但不知何故我仍然落后。

这是我创建的 jquery

$(document).ready(function()
{
var array_ids = [];
$('.add').click(function()
{
array_ids.push($(this).parent().siblings('.row_id').html().trim());
alert(array_ids);
});

$('.show').click(function(e)
{
//e.preventDefault();
var jsonString = JSON.stringify(array_ids);
$.ajax(
{
method: 'POST',
url: 'addsale.php',
data: {data : jsonString},
cache: false,
dataType: "json",
success: function()
{
console.log(data.reply);
alert(data.reply);
}
});
});
});

和addsale.php

if(isset($_POST['push'])) //tried it commenting also!
{
$data = array();
$data = json_decode(stripslashes($_POST['data']));
foreach($data as $d){
echo $d;
}
}

谁能告诉我缺少什么来访问数组并将 html 从 addsale.php 获取到当前页面?

最佳答案

$(document).ready(function()
{
var array_ids = [];
$('.add').click(function()
{
array_ids.push($(this).parent().siblings('.row_id').html().trim());
alert(array_ids);
});

$('.show').click(function(e)
{
//e.preventDefault();
//prefer parse function
var jsonString = JSON.stringify(array_ids);
$.ajax(
{
method: 'POST',
url: 'addsale.php',
data: {"data" : jsonString},
cache: false,
dataType: "json",
//e is the response text from your PHP code
success: function(e)
{
//I don't know why this code
//console.log(data.reply);
//alert(data.reply);
}
});
});
});

在你的 PHP 代码中尝试

if(isset($_POST['data'])) //tried it commenting also!
{
$data = array();
$data = json_decode(stripslashes($_POST['data']));
foreach($data as $d){
echo $d;
}
}

关于javascript - 无法从 jquery 访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40121432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com