gpt4 book ai didi

ajax - socket.io变量检查

转载 作者:太空宇宙 更新时间:2023-11-04 02:43:57 25 4
gpt4 key购买 nike

好吧,基本上我在ajax中有一个变量,我想将其发送到我的socket.io服务器以检查该变量是否已经在json数组中。

Ajax :

function isUniqueEmail(email) {
//email checking script here
$.get('info/mailcheck.js' + email, function(response) {
if(response == 1) {
alert("Your email is already on our list");
}

else {
alert("We will add you shortly");
};
})
};

json 数组:

{"mail":
[
"tom@gmail.com",
"fred@gmail.com",
"bob@gmail.com"
]}

socket.io 部分是我感到困惑的地方。基本上它只需要获取变量(一封电子邮件)检查它是否已经在数组中,如果是则返回 1,如果不是则返回 0 并将其写入数组中。

最佳答案

我对node.js了解不多,但您可以使用类似于以下的函数来检查值是否在数组中,如果不是,则将值推送到数组中。

function pushToArray(value, array) {
function inArray(val, arr) {
for (var i=0; i<arr.length; i++) {
if (arr[i] == val) {
return true;
}
return false;
}
if (inArray(value, array) {
return 1;
} else {
array.push(value);
return 0;
}
}

关于ajax - socket.io变量检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8871675/

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