gpt4 book ai didi

javascript - 同时获取2个ajax数据

转载 作者:行者123 更新时间:2023-12-02 14:50:45 25 4
gpt4 key购买 nike

我的 html 上有 2 个表,我想用通过 PHP 在 mySQL 服务器上获取的数据来填充它们。如果我没有意义,请原谅我,对网站来说相对较新

我已声明我的外部 .js html <body> 内的文件

<script src="../bower_components/accounts/accounts.js"></script>
<script src="../bower_components/accounts/userinfoRetrieval.js"></script>

正如你从我的 js 中看到的文件,这些函数会在加载 html 页面时自动调用。

我猜想存在数据冲突,因为我同时从 2 个 php 文件获取数据。因此我的表值无法正确显示。我怎样才能获取数据并在页面加载时显示它们?

account.js

var tableContents;
var recipients = new Array();
getRecipients();

function getRecipients() { //get account related info
$.ajax({
type: "GET",
url: "../bower_components/accounts/accounts.php",
dataType: "json",
data: {
json: JSON.stringify(recipients),
},
success: function(response){
recipients = response;
printData(recipients);
}
});
}

function printData(recipients){
var x = "0";

for(var i in recipients){
if (i%2 == 0){
tableContents = $("<tr class=\"even gradeC\">");
jQuery('#accountsBody').append(tableContents);

}
else{
tableContents = $("<tr class=\"odd gradeX\">");
jQuery('#accountsBody').append(tableContents);
}
tableContents.append($("<td style=\"padding-left:18px\"><input name=\"select\" type=\"checkbox\" value=\""+ i + "\"></td>"));
tableContents.append($("<td style=\"padding-left:18px\">" + recipients[i].recipientId + "</td>"));
tableContents.append($("<td style=\"padding-left:18px\">" + recipients[i].username + "</td>"));
tableContents.append($("<td style=\"padding-left:18px\">" + recipients[i].email + "</td>"));
tableContents.append($("<td style=\"padding-left:18px\">" + recipients[i].phoneNo + "</td>"));

if (recipients[i].status === x){
tableContents.append($("<td style=\"padding-left:18px\">Unapproved</td>"));
}
else{
tableContents.append($("<td style=\"padding-left:18px\">Approved</td>"));
}
tableContents.append($("</tr>"));
}
sorting();
}

userinfoRetrieval.js

var tableContents;
var userInfo = new Array();

getuserInfo();


function getuserInfo() { //get account related info
$.ajax({
type: "GET",
url: "../bower_components/accounts/userinfoRetrieval.php",
dataType: "json",
data: {
json: JSON.stringify(userInfo),
},
success: function(response){
userInfo = response;
printData(userInfo);
}
});

}

function printData(userInfo){
tableContents = $("<tr>");
jQuery('#userinfoBody').append(tableContents);

tableContents.append($("<td>" + userInfo[0].userID + "</td>"));
tableContents.append($("<td>" + userInfo[0].username + "</td>"));
tableContents.append($("<td>" + userInfo[0].email + "</td>"));
tableContents.append($("<td>" + userInfo[0].dob + "</td>"));
tableContents.append($("<td>" + userInfo[0].phoneNo + "</td>"));

tableContents.append($("</tr>"));
}

enter image description here

第一行(elijah)应该放置在最上面的 table 上,而不是当前错误放置的位置。因此存在未定义的值

最佳答案

我担心 printData 在全局范围内声明了两次,请尝试重命名它们以避免命名冲突。

我建议使用 IIFE 并使用 strict 以避免错误地污染全局命名空间。

IIFE: https://en.wikipedia.org/wiki/Immediately-invoked_function_expression

使用严格: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

关于javascript - 同时获取2个ajax数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36201259/

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