所以,我做了大量研究,大多数例子我都看过:
或
我的代码发生了什么,该值意味着是一个字符串,并且可以通过查看具有有效字符串条目的表单来进行可视化验证。代码返回值2,没有抛出错误。试图通过Chrome的控制台找到变量的值返回undefined。但是,在同一个表单上,函数会根据发送到PHP处理文件的内容生成一个正确传递的唯一ID。
这是html代码段:
<input type="text" id="billing-email">
此字段包含有效的电子邮件条目,即this@that.com
用于捕获信息的javascript文件如下所示:
$(document).ready(function() {
function getemail(){document.getElementById("billing-email").value;}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
var email = window.setInterval(getemail,100);
var uuid = guid();
function submitform(){
$.ajax({
type: "POST",
url: 'https://app.batteryjunction.com/cr/previous-builds/jstorage-test/controller/process.php',
dataType: 'json',
data: {
myform:[{
uuid: uuid,
email: email,
}]
},
async: false,
});
}
window.setInterval(submitform,3000);
});
发送到PHP处理文件的标题如下所示:
Request URL: https://some.com/some/app.php
Request Method: POST
Status Code: 200 OK
Form Data
myform[0][uuid]: ghjkdkgh-f456-75fd-5gsr-jgdk8vhd
myform[0][email: 2
问题是,为什么发送2而不是电子邮件地址?
答案 0 :(得分:1)
正如评论中j08691指出的那样,您正在存储setInterval
ID(显然是2
)。以下是更好的方法:
$(document).ready(function () {
function getemail() {
// this function doesn't return anything, so "var email = getemail();" means "var email" would be undefined
document.getElementById("billing-email").value;
}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
// window.setInterval returns an integer value representing the interval
// so you can use clearInterval to stop the interval
// correct use of "email" below would be: window.clearInterval(email);
//var email = window.setInterval(getemail, 100);
//var uuid = guid();
function submitform() {
var email = document.getElementById("billing-email").value,
uuid = guid();
$.ajax({
"type": "POST",
"url": "https://app.batteryjunction.com/cr/previous-builds/jstorage-test/controller/process.php",
"dataType": "json",
"data": {
"myform": [{
"uuid": uuid,
"email": email //remove the last comma, or IE will throw a hissy-fit
}]
},
"async": false,
});
}
window.setInterval(submitform, 3000);
});