gpt4 book ai didi

node.js - 在 View 中调用时,如何在 ejs View 中显示来自 ajax 结果集的数据

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

我有一个空的 ejs View ,但我想显示我在 ejs View 中调用的 ajax 调用的结果集

<script>
// Fetch
$.ajax(`/x`).done(function(data, textStatus, xhr){
console.log(data);
console.log(textStatus);
console.log(xhr);
});

</script>
<!-- how do I loop the data received from above and display the content here? -->

我不知道该怎么做。

更新

这就是我现在所拥有的。有没有更简洁的方法来执行此操作而不是附加为 html?

<script>
const gId = '<%= gId %>';

// Fetch the current users c here from `/g/${gId }/c `
$.ajax(`/g/${gId }/c `).done(function(data, textStatus, xhr){
var c = '';
$.each(data, function(index, value){
console.log(index);
console.log(value)
c += '<div class="col-sm-6 col-md-2"><div><img class="col-sm-12 col-md-11" src="'+value.image+'" /></div><div class="text-center"><input name="card" id="card" type="checkbox" value="'+value.value+'" /> Discard</div></div>';
});

$('#c ').append('<div class="row"><div class="row col-sm-12">'+c +'</div><div class="col-xs-2"></div></div>');

$('#c ').append('<button type="submit">c </button>');
});

const checkExchangeStatus = function(){
$.ajax(`/g/${gId}/allExchanged`).done(function(data, textStatus, xhr){
if (xhr.status !== 202) { // not pending
document.location = `/g/${gId}/result`;
}
})
}

setInterval(checkExchangeStatus, 1000);
</script>
<form method="POST" action="/g/<%= gId %>/exchange">
<div id="c ">
</div>
<div>
<br/>gId : <%= gId %>
</div>
</form>

最佳答案

"ejs code like <% %> is executed on server and rendered as html. So you can't use ejs code with ajax requests."

您可以做的是进行 ajax 调用,然后使用 jQuery 更改您的 html 页面。

我有一个空的 ejs View ,但我想显示我在 ejs View 中调用的 ajax 调用的结果集。

<script>
$.ajax(`/x`).done(function(data, textStatus, xhr){
console.log(data); //I am assuming this prints
$('#someID').html(data);
});
</script>

我不知道您期望什么结果,也不知道您的 html 是什么样子。如果您使用更多代码更新您的问题,我可以进一步帮助您。

关于node.js - 在 View 中调用时,如何在 ejs View 中显示来自 ajax 结果集的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56385700/

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