我在互联网上搜索后问这个问题。我用JEE服务器和jsps做了很多。我正在尝试使用Node和JQuery并且很难让它工作。我有一个获取输入的表单,我向服务器发送Ajax帖子,服务器通过基于输入呈现视图来响应。在我看来,我不必担心客户端上的click事件附带弹出窗口。对于我的生活,我无法显示服务器响应输入发送的页面。我在这里错过了什么吗?任何关于这个用例的智慧珍珠都将不胜感激。我尝试了一些相关的例子,他们没有看到工作。这是代码。从服务器返回时,不呈现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' });});
答案 0 :(得分:1)
听起来像是在寻找$.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或模板。
答案 1 :(得分:1)
除了robertotomás的回答,还要确保您的服务器启用了CORS支持。这在以下情况下特别有用:
有用的资源:
答案 2 :(得分:0)
所以在我的情况下,我必须有一个div元素,我可以运行html方法。因此,在我的html页面的主体中创建了一个div元素,我能够做一个$(" div.listcontainer")。html(data);我的页面显示得很好。