如何在不重新加载页面的情况下从数据库获取数据? [英] How to get data form database without reloading the page?

查看:98
本文介绍了如何在不重新加载页面的情况下从数据库获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户询问它在分区顶部显示的任何问题而不重新加载页面时,我想创建一个faq面板我认为我必须在每隔几秒后与数据库建立连接。现在问题是如何在不重新加载页面的情况下建立连接以及如何显示新问题?

I want to make a faq panel when user ask any question it show on the top of division without reloading the page for that i think i have to make a connection with database after every few second. Now the question is that how to make connection without reloading the page and how to show new question ?

推荐答案

您有两种选择:


  1. Ajax ,它允许您使用JavaScript从服务器检索数据,然后您可以使用它来操作DOM。 Ajax的基础是 XMLHttpRequest 对象,它允许您在JavaScript中完全在幕后检索数据。请注意,Ajax受到同源策略的限制,但对于您所描述的内容,这很好  ;—你将从同一个来源加载数据。

  1. Ajax, which allows you to retrieve data from the server with JavaScript, which you can then use to manipulate the DOM. The basis of Ajax is the XMLHttpRequest object, which allows you to retrieve data completely behind-the-scenes in JavaScript. Note that Ajax is limited by the Same Origin Policy, but for what you're describing, that's fine — you'll be loading data from the same origin.

框架(例如, iframe 元素),您可以通过设置 src 属性来加载内容。

Frames (e.g., iframe elements), which you can load content into by setting their src property.

在这两者中,Ajax更灵活。

Of the two, Ajax is much more flexible.

参考/进一步阅读:

  • DOM2 Core
  • DOM2 HTML
  • DOM3 Core
  • HTML5 Web Applications APIs
  • XMLHttpRequest object

附注:虽然显然你可以直接使用 XMLHttpRequest 和DOM方法,但请注意跨浏览器不同可以通过像 jQuery 原型 YUI ,< a href =http://code.google.com/closure/library =nofollow>关闭,或其中任何一个。它们还提供了许多有用的实用功能,使您可以专注于您尝试解决的实际问题,而不是管道的细节。

Side note: Although obviously you can use XMLHttpRequest and the DOM methods directly, note that there are cross-browser differences (and outright bugs), which can be smoothed over for you by a good library like jQuery, Prototype, YUI, Closure, or any of several others. They also provide a lot of useful utility functionality, allowing you to focus on the actual problem you're trying to solve rather than the details of the plumbing.

例如,以下是使用jQuery向服务器发送Ajax请求的方法,并且页面上的元素使用服务器发回的HTML片段进行更新:

For example, here's how you would send an Ajax request to a server using jQuery and have an element on the page updated with the HTML fragment the server sends back:

$("#target").load("get_the_data.php", {article: x});

这就是说:从请求一个HTML片段get_the_data.php 使用 x 变量中的值向参数 article 发送,并将该HTML片段放入元素中它具有HTML id target。如果您不使用库,这将是一个很好的10行代码。现在,这不是很多,但重复一遍又一遍(并在此过程中处理IE浏览器的错误,围绕按 id 查找元素),你看到它是怎么回事可以加起来。

That says: Request an HTML fragment from get_the_data.php sending it the parameter article with the value from the x variable, and put that HTML fragment inside the element that has the HTML id "target". That would be a good 10 lines of code if you didn't use a library. Now, that's not a lot, but repeat that over and over (and in the process deal with an IE bug around looking up elements by their id), and you see how it can add up.

我建议你阅读上面的参考资料,这样你就知道图书馆正在做他们正在做的事情(这不是魔术),但是没有理由不要利用人们为使这些事情变得更容易所做的努力。

I do recommend you read the references above so you know how the libraries are doing what they're doing (it's not magic), but there's no reason not to leverage the hard work people have done to make this stuff easier.

这篇关于如何在不重新加载页面的情况下从数据库获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆