用 jquery $.post 响应替换页面
我正在使用jquery的$.post,如下所示:
$.post("/searchresult", $("#searchDiv").serialize(), function(data) { //??? });
“data”参数包含我想要的结果页面的所有html代码。
问题是如何用 $.post 函数返回的内容替换当前页面的内容。
谢谢,
瑞
I'm using jquery's $.post like this:
$.post("/searchresult", $("#searchDiv").serialize(), function(data) { //??? });
The "data" parameter has all the html code of the result page I want.
The question is how do I replace my current page's content with this one returned by the $.post function.
Thanks,
Rui
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
$('body').html(data);
应该这样做。$('body').html(data);
should do it.您可以使用:
You can use:
我已经通过多次 ajax 调用来完成此操作,以使用
$('body').empty()
重新填充页面,然后使用几个$('body').appendTo(...) 。
假设您可以安排您的Web服务单独返回body的innerHtml(而不是整个html文档),我认为只需
$('body').html(data)
应该可以工作 如果没有,我会考虑使用 src 的 url 或类似的 IFRAME 加载正文I've done this with multiple ajax calls to refill a page using
$('body').empty()
and then several$('body').appendTo(...)
Assuming you can arrange for your webservice to return innerHtml of body alone (and not an entire html document), I think just
$('body').html(data)
should work. if not, i would consider loading the body with an IFRAME that src's that url.. or similar回答有点晚了,但这看起来是将我的解决方案添加到您遇到的类似问题的最佳位置。
有类似的情况,需要使用.ajax POST html响应,它包含错误消息。
问题始于 Chrome 浏览器,提交后不允许异步发布。我们有一个长期运行的流程,需要显示状态。因此,我们的状态跟踪器在提交发生时进行 asycn 调用,在大多数浏览器中工作,但最近在 Chrome 中停止工作。
- 一种解决方案是使用 iframe 并以这种方式调用其他服务器端代码。对我来说不是一个选择。
-或者更改服务器端代码以返回 json 并以这种方式处理内容,需要重新编写已经到位的客户端和服务器代码。
我尝试解决 Chrome 的异步调用问题,同时提交帖子并处理 Html 响应。
这是我想出的,并且似乎正在发挥作用。
去了这么多的网站,得到了这么多的想法和知识,我不能直接归功于任何人。
所以我基本上把 $(form).submit() 变成了 ajax post。但这在 Chrome 和其他浏览器尝试处理返回的 html 响应时仍然存在问题。
最大的问题是重新连接按钮事件和 JavaScript。
希望这会对某人有所帮助。
Little late for answering, but this looked like the best place to add my solution to s similar problem you were having.
Had a similar situation, need to use the .ajax POST html response, it contained error messages.
Problem that started with Chrome browser, not allowing async posts after a submit. We had a long running process and needed to show status. So our status tracker was making asycn calls while the submit was occurring, worked in most browser, but recently stopped working in chrome.
-One solution would be to use an iframe and call the other server side code that way. Was not an option for me.
-Or change the server side code to return json and handle the stuff that way, lot more work re-writing client and server code already in place.
I Went down the road of trying to fix the async call problem with Chrome while making a submit post and handle the Html response.
This is what i came up with, and seems to be working.
Went to so many sites and got so many ideas and knowledge, i can't give direct credit to anyone.
So i basically turned the $(form).submit() into an ajax post. But this still has trouble in chrome and other browsers trying to handle the returned html response.
The biggest issue, re-wiring the button events and the javascript.
Hopefully this will help someone.
我发现有效的是将表单动态附加到正文并在其之后立即提交。通过这种方式,您可以使用传统的帖子,而无需在页面上显示表单。
What I've found working is the form being appended to body dynamically and submitted just right after it. This way you utilize traditional post w/o showing the form on the page.