Javascript 获取表单数据,并使用ajax将其传输到表中
我试图从php文件中获取数据并将其显示在表中。我一直在尝试遍历每一行数据,然后将其追加到表中的下一行,但我遇到了使用forEach循环的障碍。很明显,我没有正确地使用循环,在控制台中,我得到一个错误,声明“uncaughttypeerror:data.forEach不是函数”。有没有人能为我指出正确的方向,告诉我我到底做了哪些错误,和/或我如何更容易找到解决方案。还有谁能解释一下,我是如何得到这个文件的数据作为它自己的变量的,因为某种原因,我做这个有困难。请原谅我在代码中使用了不正确的术语或一些非常可能的方法误用。顺便说一下,我不控制这个服务器 以下是我在按下更新按钮更新表格时的函数代码Javascript 获取表单数据,并使用ajax将其传输到表中,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我试图从php文件中获取数据并将其显示在表中。我一直在尝试遍历每一行数据,然后将其追加到表中的下一行,但我遇到了使用forEach循环的障碍。很明显,我没有正确地使用循环,在控制台中,我得到一个错误,声明“uncaughttypeerror:data.forEach不是函数”。有没有人能为我指出正确的方向,告诉我我到底做了哪些错误,和/或我如何更容易找到解决方案。还有谁能解释一下,我是如何得到这个文件的数据作为它自己的变量的,因为某种原因,我做这个有困难。请原谅我在代码中使用了不正确的术语或一些
function updateButton(){
$.get( "php link", function(data, status) {
$.ajax({
url: "php link",
data: data,
success: data.forEach(function(row) {
var htmltext = "<tr><td>"+row.lastname+"</td><td>"+row.currentcity+"</td><td>"+row.uid+"</td><td>"+row.ip+"</td><td>"+row.updateTime+"</td></tr>"
$(("#table1 tbody").append($(htmltext)));
}),
dataType: JSON
});
});
}
函数updateButton(){
$.get(“php链接”,函数(数据、状态){
$.ajax({
url:“php链接”,
数据:数据,
成功:data.forEach(函数(行){
var htmltext=“”+行.lastname+“”+行.currentcity+“”+行.uid+“”+行.ip+“”+行.updateTime+“”
$(“#表1 tbody”)。追加($(htmltext));
}),
数据类型:JSON
});
});
}
您需要正确处理成功方法。大概是这样的:
success: function(resultData) {
resultData.forEach(function(row) {
console.log(row.lastname);
console.log(row.currentcity);
console.log(row.uid);
console.log(row.ip);
console.log(row.updateTime);
console.log(row);
var htmltext = "<tr><td>"+row.lastname+"</td><td>"+row.currentcity+"</td><td>"+row.uid+"</td><td>"+row.ip+"</td><td>"+row.updateTime+"</td></tr>";
$(("#table1 tbody").append($(htmltext)));
});
}
success:函数(resultData){
resultData.forEach(函数(行){
console.log(row.lastname);
console.log(row.currentcity);
console.log(row.uid);
console.log(row.ip);
console.log(row.updateTime);
控制台日志(行);
var htmltext=“”+行.lastname+“”+行.currentcity+“”+行.uid+“”+行.ip+“”+行.updateTime+“”;
$(“#表1 tbody”)。追加($(htmltext));
});
}
尝试在控制台面板中查看输出内容 像这样试试
function updateButton(){
$.get("php link", function(data, status){
$.ajax({
type:"POST",
dataType: "JSON",
url: "php link",
data: data,
success: function(data) {
$.each(data,function)(index,row){
var htmltext = "<tr><td>"+row.lastname+"</td><td>"+row.currentcity+"</td><td>"+row.uid+"</td><td>"+row.ip+"</td><td>"+row.updateTime+"</td></tr>"
$(("#table1 tbody").append(htmltext));
});
}
});
});
函数updateButton(){
$.get(“php链接”,函数(数据、状态){
$.ajax({
类型:“POST”,
数据类型:“JSON”,
url:“php链接”,
数据:数据,
成功:功能(数据){
$.each(数据、函数)(索引、行){
var htmltext=“”+行.lastname+“”+行.currentcity+“”+行.uid+“”+行.ip+“”+行.updateTime+“”
$(“#表1 tbody”).append(htmltext));
});
}
});
});
现在,我在控制台中收到一个错误,声明“uncaughttypeerror:(o.dataType | |“*”)”。toLowerCase不是一个函数我真的不知道为什么会发生这种情况。什么是o
,什么是o.dataType
?我没有任何名为o的变量。我不知道这个错误的意思可能是你试图访问不在那里的成员。检查数据中成员的拼写和大小写。我注意到你正在使用camelCasing来更新时间,但是所有其他currentcity和lastname的大小写。是lastname还是lastname?