我在网上搜索了一番后才问这个问题。我经常使用 JEE 服务器和 jsp 来完成此操作。我正在尝试使用 Node 和 JQuery,但很难让它工作。我有一个获取输入的表单,我向服务器发送 Ajax 消息,服务器通过根据输入呈现 View 来进行响应。在我看来,我不必担心客户端上单击事件附加的弹出窗口。在我的一生中,我无法显示服务器响应输入而发送的页面。我在这里错过了什么吗?任何有关此用例的智慧珍珠都将不胜感激。我尝试了一些相关的例子,但它们似乎不起作用。这是代码。从服务器返回后,不会呈现 HTML。我只是添加执行 AJAX 发布并处理来自服务器的响应的 javascript。
script.
$(document).ready(function() {
$("#dialog").dialog({
autoOpen: false,
modal: true,
title: "Details",
buttons: {
Close: function () {
$(this).dialog('close');
}
}
});
$("#OK").click(function() {
var org = $("#name").val();
if (org == '' ){
alert("Please Fill Required Fields");
} else {
alert("about to post "+org);
$.post("/repost",
{name: org})
.done(function(data) {
alert(data);
$("#dialog").html(data);
$("#dialog").dialog("open");
});
$("#form")[0].reset();
}
});
$("#cancel").click(function() {
$("#form").dialog("close"); // To close the form
});
});
服务器代码:
app.post('/repost', function(req, res) {
console.log("We are called with ", req.body);
res.render('reposub.jade', {org:req.body.name, title: 'Express' });});
最佳答案
听起来您正在寻求有关 $.post
的帮助:
$.post( "ajax/test.html", function( tx_response ) {
//console.dir(tx_reponse) -- does this have ususal stuff like _data_ and _status_? Or is is a raw HTML response?
if (/* some condition like: tx_response.status === 200 */) {
// if the html is a template, and not a redirect to a new url:
$('.result').html(tx_response.data)
// redirect:
window.location.href = tx_response.data
//...
});
如果它既不是重定向也不是模板,而是客户端应该加载的带有 HTML 的全新 http 回复,我认为您可以使用 document.write
但是..这可以被设计在这种情况下更好。可以说,响应最多应该是一个重定向 URL 或一个模板。
关于javascript - 在 AJAX 发布后显示服务器发送的 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39184131/