gpt4 book ai didi

javascript - JQuery - 将twodim 数组传递给$.post 失败

转载 作者:行者123 更新时间:2023-12-03 02:42:48 25 4
gpt4 key购买 nike

在一个函数中,我创建一个数组并尝试使用 jQuery.post 发布该数组 - 但该数组从未发布。

数组“recipients”填充了从“.smstag”收集的数据,然后我尝试将数组发布到我的服务器,服务器实际上只响应 $_POST 的 json 编码数组。

我在发布之前console.log“recipients”并看到3个收件人,当$.post成功时我console.log(json)并只看到“smsmsg”变量 - 看起来我从未发布过收件人。

(3) [Array(0), Array(0), Array(0)]
0
:
[cellphone: "+4588888888", username: "Mark", id: "5"]
1
:
[cellphone: "+4588888888", username: "Marie", id: "227"]
2
:
[cellphone: "+4588888888", username: "Marie", id: "129"]
length
:
3
__proto__
:
Array(0)

function sendSms(){
if($("#smsrecipients").find(".smstag").length==0)return;
if($("#smsmsg").val().length==0)return;
var recipients=[];
$("#smsrecipients").find(".smstag").each(function(){
var u=[];
u["cellphone"]=$(this).attr("cellphone");
u["username"]=$(this).attr("username");
u["id"]=$(this).attr("userid");
recipients.push( u );
});
swal({
title: '<?php echo _t("Send message now?")?>',
type: 'question',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: '<?php echo _t("Yes, send it!")?>',
cancelButtonText: '<?php echo _t("Cancel")?>',
showLoaderOnConfirm : true,
}).then((result) => {
console.log(recipients);
$.post("/ajax/sendsms",{"recipients":recipients,"smsmsg":$("#smsmsg").val()},function(json){
if(json.success){
console.log(json);
return true;
}
},"json");
})
}

服务器返回的JSON结果:

{success: true, post: {…}}
post
:
smsmsg
:
"Kære #name#↵Du får lige en testbesked!!↵Dette er lige skrevet!!"
__proto__
:
Object
success
:
true
__proto__
:
Object

我不知道我错在哪里,非常感谢任何帮助!

最诚挚的问候,标记

最佳答案

javascript 数组仅按数字索引

您正在尝试使用 u 作为对象,因此首先将其设置为对象文字而不是数组

var u=[]; 更改为 var u={};

关于javascript - JQuery - 将twodim 数组传递给$.post 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48249659/

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