使用 jQuery.post() 提交内容并显示发生的情况
这比任何事情都更令人好奇,但我可以想到几个地方(主要是在管理部分)这可能有用。我知道标题很令人困惑,所以这就是我的意思:
假设您有一个发布到 example_cb.php 的表单。 example_cb.php 是一个非常大的回调页面,它生成图像的多个缩略图,将它们放在各自的目录中,将它们添加到数据库等。您已经有了由 jquery 发布的表单,其功能如下
$.post('example_cb.php', $(this).serialize(), function(data) {
// Success
}, 'json');
在发布之前更新div 表示某物的“处理”。然后,在成功完成后,div 就会淡出。但是您希望 div 显示服务器端发生的情况。即转换200X200拇指>转换 350x350 拇指 >将记录添加到数据库>等等。
你怎样才能做到这一点?
我知道这是一个沉重的问题,但我相信这里有人足够聪明,能够解决这个问题。如果您需要更多信息,请评论:)
This is more of curiosity that anything, but I can think of several places (mainly in admin sections) where this could be useful. I know the title is confusing so this is what I mean:
Say you have a form that posts to example_cb.php. example_cb.php is a very large callback page that generates multiple thumbnails of an image, puts them in their respective directories, adds them to a database, etc. You already have the form posting by jquery with a function like this
$.post('example_cb.php', $(this).serialize(), function(data) {
// Success
}, 'json');
Before posting it updates a div to say "processing" of something. Then after it successfully completes the div fades out. However you want the div to show what is happening on the server side. i.e. Converting 200X200 thumb > Converting 350x350 thumb > Adding Records to Database > etc.
How can you accomplish this?
I know it's a loaded question, but I'm confident there are people on here smart enough to figure it out. If you need more info please comment :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行类似的操作 -
随着更新的进展。
You could do something like -
with the updated progress.
使用推送方法可以最轻松地完成此任务,但我不太熟悉ajax推送或comet技术,所以我会给你一个拉解决方案。
基本上,您需要创建初始 ajax 请求才能开始处理。然后,您需要在紧密循环中运行另一个请求,以检查 php 页面的当前状态。例如,在进行处理的 php 页面上,您可以在每一步更新存储当前处理信息(例如“转换 200X200 拇指”)的
_SESSION
键。另一个 php 页面的存在纯粹是为了打印该信息,您可以通过循环运行的 JS 来检索它。This could be accomplished most easily with using the push method, but I'm not too familiar with ajax push or comet technology, so I'll give you a pull solution.
Basically you need to create the initial ajax request to start the processing. Then, you need to have another request running in a tight loop that checks against a php page for the current status. For example, on the php page doing the processing, you can update a
_SESSION
key that stores the current processing information (like "Converting 200X200 thumb") at each step. Another php page will exist purely to print that information and you can retrieve it with the JS running in a loop.pusher 之类的服务可用于双向通信。
我把它写下来供参考,尽管对于如此简单的场景来说这有点过分了。
pusher like services may be used for bi-directional communication.
I'm putting it down for reference, though this would be overkill for such a simple scenerio.